c++ - Multiple use of same file streaming object -
i've written following source code:
ifstream leggifile; leggifile.open("questions.txt",ios::in); if (!leggifile.good()) { cerr << "\n\n\n\terrore during file opening questions.txt\n\n\n" << endl; } else { // ... }; leggifile.close(); system("pause");
now i'd use same object working second file.
leggifile.open("answers.txt",ios::in); i=0; if(!leggifile.good()) { cerr << "\n\n\n\terror during opening of file answers.txt\n\n\n" << endl; } else { // ... }
problem: 2nd time file cannot opened , error message appears. why? please suggest me solution?
it's possible you've done work on stream set 1 or more of error flags, such eofbit
.
closing stream doesn't clear error flags, have manually. call leggifile.clear();
after close it.
since c++11, done automaticaly open()
, though. if you're using c++11 compiler, problem elsewhere (can't where, haven't shown enough code).
Comments
Post a Comment