简体   繁体   中英

Writing using << on an fstream

I'm trying to write a long to a text file using c++ fstream class. The file is already created on disk before the execution. I run the following code and can read the initial value but can't save the new one, overwriting. What am i doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>


using namespace std;

int main(int argc, char* argv[]) {

    long f;

    fstream myFile("data.txt", fstream::in|fstream::out);
    cout << "f before: " << f << endl;
    myFile >> f;
    cout << "f after: " << f << endl;
    f++;
    cout << "f after increment: " << f << endl;

    myFile << f;
    myFile.close();

    return 0;
}

After that, I read the value in the file and it isn't changed. What I did wrong here?

You need to rewind to the beginning of the file before writing. Otherwise the second value is written after the first one.

You need to add myFile.seekp(ios::beg); just before myFile << f; in order to update the count correctly.

If you want to keep appending to the end, add myFile.clear(); before myFile << f; . This will cause the contents to become : 1 -> 12 -> 1213 -> 12131214 -> 1213121412131215 . This is required because eof is reached upon reading the input. Note that get and put pointers are the same .

As you have yourself correctly pointed out, this is required because the file has just the number, not even the newline. Thus the read operation hits straight the EOF and causes problems. To work around it, we clear eof status and continue.

Adding a newline at the end is a solution as you suggested. In that case

myFile.seekp(ios::beg);
myFile << f<<"\n";

Would be the complete solution.

I figured what I was doing wrong here:

My file contained ONLY the long that I wanted to read and write afterwards. When I read the file, I reached EOF and then couldn't rewind or write anything at the end.

That said, my solution was to include a space or a \\n at the end of the file.

Does anyone know why the API works this way? Does not seeem very useful for me...

It has to do with this line:

myFile >> f;

Remove it and everything works just fine. I'm not familiar with fstream but it seems to me this code would try to force a string in a long. I'm also not allowed to cast it to a long, which makes me think this is never meant to be executed like this. I suggest you read up on how to retrieve a value from a file as a long type and then try again.

edit:
After reading a bit this site suggested you have to close and reopen the file between reading and writing, I was amazed that actually fixed it. I can't help but wonder why though, I thought fstream was meant for reading OR writing OR both.... Anyway, here is your working code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {

    long f;

    fstream myFile("data.txt", fstream::in|fstream::out);
    cout << "f before: " << f << endl;
    myFile >> f;
    cout << "f after: " << f << endl;
    f++;
    cout << "f after increment: " << f << endl;
    myFile.close();
    myFile.open("data.txt", fstream::in|fstream::out);

    myFile << f;
    myFile.close();

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM