Can you use the same ifstream on multiple files?
Example:
ifstream input_file( "file.txt" );
input_file.close();
ifstream input_file( "file2.txt" );
This gives an error.
Is there any way to make it work?
Printable View
Can you use the same ifstream on multiple files?
Example:
ifstream input_file( "file.txt" );
input_file.close();
ifstream input_file( "file2.txt" );
This gives an error.
Is there any way to make it work?
You should call clear() as well after close().
gg
You can't declare two variables of the same name in the same scope. So the second one would just need to be an open call, not a constructor.
But there probably isn't much reason to go through all the trouble of reusing the fstream.
Yeah woops, I mistyped. Here's what I have now:
But still,Code:ifstream input_file( "file.txt" );
input_file.close();
input_file.clear();
input_file( "file2.txt" ); // Line 57
I have to open about 20 files and do various things in each. Is there a way do reuse the same ifstream, or do I have to make 20 different ifstream variables?Code:ex2.cpp:57: error: expected `;' before ‘open’
ex2.cpp:57: warning: statement has no effect