Auberotte
Auberotte

Reputation: 221

C++: Why is ofstream not appending or creating new files?

i have a little problem with ofstream. My main calls a certain class several times, each time with different parameters for testing purposes

ImageComparison* imco = new ImageComparison(queries[i], j, k, l);

Inside each instance i want a formatted output to a file, so i first tried this

ofstream ofs;
ofs.open("somepath" + params + ".txt");
ofs << "write results";
ofs.close();

I expected that ofstream would create several files, due to different params and therefor different filenames, and write something into it. But it appears that it's always overwriting the former file and just saving the last instance.
Afterwards i tried to use one file over and over again and appending new lines, since this would be a better solution for my case

ofstream ofs;
ofs.open("somename.txt", ofstream::out | ofstream::app);
ofs << "write params";
ofs << "write results in same line";
ofs << endl;
ofs.close();

In this case it appears that it's not appending new lines, instead it overwrites the former line and in the end i just have the results of the last instance.
Please enlighten me, i don't have any ideas whats wrong here.
I appreciate any suggestions/solutions :)
EDIT: here is concrete case:
main:

vector<string> queries = {"apple","banana","book"...};
for(int i=0; i<18; i++) {
    //if(i==1) break;
    for(int j=0; j<3;) {
        //if(j==2) break;
        for(int k=10; k<800;) {
            //if(k==400) break;
            for(int l=50; l<600;) {
                ImageComparison* imco = new ImageComparison(queries[i], j, k, l);
                imco->DoImCo();
                delete imco;
            }
        }
    }
}

ImageComparison.cc:

string bloo = "../ImageData/" + m_object_type + "/" + m_object_type + "metric=" + to_string(m_metric) + ",hessian=" + to_string(m_hessian) + ",words" + to_string(m_number_of_words) + ".txt";
ofstream ofs;
ofs.open(bloo.c_str(), ofstream::out | ofstream::app);
for(int i=0; i<image_names.size(); i++) {
    if(similarity_of_one[i] < similarity_average) {
        ofs << " x";
    }else{
        ofs << " v";
    }
}
ofs << endl;
ofs.close();

EDIT2: forgot parameter increase inside the "for's" but not important i think

Upvotes: 2

Views: 6762

Answers (2)

Auberotte
Auberotte

Reputation: 221

Finally, i found something. It seems that there were a problem with the name. After using a constant name like "../Test.txt" it worked fine, sill don't know why but the problem is gone. Big thx for all your suggestions.

Upvotes: -1

LittlePrince
LittlePrince

Reputation: 41

Did you try starting a new project with only this part of your example code? Doesn't it append new lines for you?

Blockquote

ofstream ofs;
ofs.open("somename.txt", ofstream::out | ofstream::app);
ofs << "write params";
ofs << "write results in same line";
ofs << endl;
ofs.close();

Try to use ofstream::app only, it will automatically create new file for you. Frankly, I don't see any problem with the above code.

(Sorry I never made an answer. It looks lame :( )

Upvotes: 2

Related Questions