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?
This is a discussion on Simple ifstream Question within the C++ Programming forums, part of the General Programming Boards category; Can you use the same ifstream on multiple files? Example: ifstream input_file( "file.txt" ); input_file.close(); ifstream input_file( "file2.txt" ); This ...
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.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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