PDA

View Full Version : fstream open/close



mickey
12th October 2006, 18:17
hi, my app is similar this below:


fstream file;
while ((choice=getchar()) != '0' ) {
if (!file.open(file_name.c_str(), std::ios_base::in)) { //read only
if ((file()).eof())
cout << "end file \n";
cerr << "unable to open that file\n";
return -1;
}
while ( c=file.get(); ) {
cout.put(ch);
}
f.close();
}

I read all file and at the end of first cycle I close the file; but the second cycle, program terminate because file seems to be at the end. Why? I supposed if I close and re-open it goes at begin....thanks

high_flyer
13th October 2006, 09:23
maybe the while loop is faster than the disk operation.
Try to set a sleep() after f.close().

mickey
13th October 2006, 10:08
how can it be faster?


while ((choice=getchar()) != '0' ) {

here, Program wait for an input......

high_flyer
13th October 2006, 12:12
try f.flush() before the close then...

mickey
13th October 2006, 12:23
try f.flush() before the close then...
doesn't work but this is ok:


file.clear();
file.close()