Comparision of ofstream object and double/integer variable in If statement

I am writing a program in C++ in which I need the comparsion of ofstream object with a double/int variable but the compiler gives me error that 'No operator == matches these operands, operands types are double == std::ofstream' my code is this.
void assign_clusters(double clusters_assigned[N], double X[N][M])
{
ofstream output[K]; //Creating array of K number of output file streams
for (int i = 0; i < K; i++)
{ //open all file streams
output[i].open(name + char('1' + i) + ".dat");
}
for (int i = 0; i < N; i++)
{
clusters_assigned[i] = find_max(Uik[i]);
if (clusters_assigned[i] == output[i])

The compiler gives error in this last line i.e. if statement and both contains values that are similar i.e. 1,1 or 2,2 etc for different iteration. Please guide me what should I do with this.
What is it that you are trying to do with the if statement if (clusters_assigned[i] == output[i])?
Are you trying to check if the stream output[i] contains the value clusters_assigned[i]?
Actually output[i] is an array of ofstream type which contains upto 6 number of items with and every time it contain values with in the range of 1 to K (2 or 3 or 4 or 5 or 6 or 7 etc i.e. only one value) and double type i.e. clusters_assigned[i] also contains that so I want to compare these because I am writting a program for Fuzzy K Means in which I should write every data item on their respective cluster (on file i.e. on hard disk) this type of comparison is necessary for this program. Any kind of type_casting etc will not work to achieve this goal.?
My goal is to write like this one...
if clusters_assigned[i] & output[i] == 1 so output[0] << cluster[0]; on file or if both are equal to 2 so it would write like output[1] << cluster[1] and so on.
Last edited on
Consider the following approach:

a. In the function, create an array of K vectors (instead of K output streams).

b. In the if statement, check if output_vector[i] contains the value clusters_assigned[i], and append the new value to the appropriate vector

c. At the end of the function, iterate through the K output vectors, and for each vector write its contents to the corresponding output file stream.
Thanks for your reply but actually output_vector[i] is for creating K number of files on hard disk simple array will not fulfill my requirement. For example if K = 5 so, my progrom is going to write 5 files with the name of Cluster1.dat, Cluster2.dat......... Cluster5.dat but I am facing the problem of writing every data points on differenct cluster i.e. to cluster1 file, cluster2 etc.
My progrom works fine with simple cout statement the code of which is :
for (int i = 0; i < N; i++)
{
clusters_assigned[i] = find_max(Uik[i]);
cout << X[i][0] << " , " << X[i][1] << " belongs to Cluster # " << clusters_assigned[i] << endl;
}

But it's mendatory to write data items on files.
> For example if K = 5 so, my progrom is going to write 5 files
> with the name of Cluster1.dat, Cluster2.dat......... Cluster5.dat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const std::size_t K = 5 ;
std::vector<double> output_vector[K] ;

// ...

// if output_vector[i] contains clusters_assigned[i]
const double delta = 1.0e-8 ;
const auto iter = std::find_if( output_vector[i].begin(), output_vector[i].end(),
                  [delta]( double a, double b ) { return std::abs(a-b) < delta ; } ) ;
if( iter != output_vector[i].end() ) // if found
{
    output_vector[i].push_back( X[i][0] ) ;
    output_vector[i].push_back( X[i][1] ) ;
    // ...
}
else
{
    // ...
}

// ...

// finally, write the contents of the vectors to files
for( const auto& vec : output_vector )
{
    std::ofstream file( "Cluster" + std::to_string(i+1) + ".dat" ) ;
    for( auto value : vec ) file << value << '\n' ;
}
Thanks, it worked.
Topic archived. No new replies allowed.