D
David
hello,
I'm trying to figure out a weird problem with opening a file for both
input and output using fstream...
compiling using g++ 4.3.2 on debian
-----
fstream f1;
f1.open("./test.txt", std::ifstream::in | std::ifstream:ut);
f1 << "1 2 3\n" << flush; //dump something into file
f1.seekg(0); //set get ptr to 0
int i;
f1 >> i; //read out '1'
//cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp()
<< endl;
f1 << "a" << flush;
if (!f1.good())
{
cout << "fstream is bad" << endl;
}
else
{
cout << "fstream is good" << endl;
}
----
If i run the code above as is, I get eof error when trying to write
'a' into stream... text.txt reads "1 2 3"
if uncomment the cout, program completes and text.txt reads "1a2 3"
according to the '98 c++ open standard there seems to be some
inconsistencies on how seekg and seekp operates, but i can't find
anything that explains this behavior...
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136
Any insights on why this is happening and how i can get this to work
without the cout is greatly appreciated!
Thanks!
~dAviD
I'm trying to figure out a weird problem with opening a file for both
input and output using fstream...
compiling using g++ 4.3.2 on debian
-----
fstream f1;
f1.open("./test.txt", std::ifstream::in | std::ifstream:ut);
f1 << "1 2 3\n" << flush; //dump something into file
f1.seekg(0); //set get ptr to 0
int i;
f1 >> i; //read out '1'
//cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp()
<< endl;
f1 << "a" << flush;
if (!f1.good())
{
cout << "fstream is bad" << endl;
}
else
{
cout << "fstream is good" << endl;
}
----
If i run the code above as is, I get eof error when trying to write
'a' into stream... text.txt reads "1 2 3"
if uncomment the cout, program completes and text.txt reads "1a2 3"
according to the '98 c++ open standard there seems to be some
inconsistencies on how seekg and seekp operates, but i can't find
anything that explains this behavior...
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136
Any insights on why this is happening and how i can get this to work
without the cout is greatly appreciated!
Thanks!
~dAviD