Thread: Simple ifstream Question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Simple ifstream Question

    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?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You should call clear() as well after close().

    gg

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But there probably isn't much reason to go through all the trouble of reusing the fstream.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Codeplug View Post
    You should call clear() as well after close().

    gg
    Quote Originally Posted by tabstop View Post
    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.
    Yeah woops, I mistyped. Here's what I have now:

    Code:
    ifstream input_file( "file.txt" );
    input_file.close();
    input_file.clear();
    
    input_file( "file2.txt" ); // Line 57
    But still,

    Code:
    ex2.cpp:57: error: expected `;' before ‘open’
    ex2.cpp:57: warning: statement has no effect
    Quote Originally Posted by anon View Post
    But there probably isn't much reason to go through all the trouble of reusing the fstream.
    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?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Paul22000 View Post
    Yeah woops, I mistyped. Here's what I have now:

    Code:
    ifstream input_file( "file.txt" );
    input_file.close();
    input_file.clear();
    
    input_file( "file2.txt" ); // Line 57
    But still,

    Code:
    ex2.cpp:57: error: expected `;' before ‘open’
    ex2.cpp:57: warning: statement has no effect


    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?
    I don't see the word "open" in that last line. Why don't I?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by tabstop View Post
    I don't see the word "open" in that last line. Why don't I?
    Woops

    I did

    Code:
    ifstream input_file( "file.txt" );
    input_file.close();
    input_file.clear();
    
    input_file open( "file2.txt" );

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Paul22000 View Post
    Woops

    I did

    Code:
    ifstream input_file( "file.txt" );
    input_file.close();
    input_file.clear();
    
    input_file open( "file2.txt" );
    Is that a direct copy-paste from the file? (If not, why not?) You need a dot between file and open.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by tabstop View Post
    You need a dot between file and open.
    AH!!!!!!!!!!!!!!!!!!!!!

    Lol

    Thank you, works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple fp_in question.
    By BluePudding in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2006, 08:18 AM
  2. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. simple File I/O question
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 06:53 AM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM