int main(){
    fstream in;
    in.open ("myFile.dat", fstream::in | fstream::out | fstream::app);
    char* temp = new char [40];
    string aString;
    in.write("123\n",4);
    getline(in, aString); //in.read(temp, 4);
    cout<<aString<<endl;
}
i want to either display the text already in myFile.dat or 123 if it was a empty file
i tested with a emtpy file, but the terminal display as a empty string, so how should i slove this problem?

Recommended Answers

All 2 Replies

After you write the string you have to rewrind the file pointer before you can read it again. See fstream's seekp() function and set the file pointer back to the beginning of the file.

thanks! i thought the compiler would do it for me~~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.