Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:07:30+00:00 2026-05-26T22:07:30+00:00

Possible Duplicate: C++ can I reuse fstream to open and write multiple files? why

  • 0

Possible Duplicate:
C++ can I reuse fstream to open and write multiple files?

why is it not possible to use one ifstream variable for opening one file, reading it, then closing it and, after that, opening another file, reading and closing, etc?
How would that look in code (let’s just say each file has an integer inside):

int k, l;  
ifstream input1;  
input1.open("File1.txt");  
input1 >> k;  
input1.close();  
input1.open("File2.txt");  
input1 >> l;  
input1.close(); 

the only way I solved the problem was creating another ifstream variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:07:30+00:00Added an answer on May 26, 2026 at 10:07 pm

    You can use the same variable, you need to call .clear() to clear the object’s flags before you reuse it:

    int k,l;
    ifstream input1;  
    input1.open("File1.txt");  
    input1 >> k;  
    input1.close();  
    input1.clear();
    input1.open("File2.txt");  
    input1 >> l;  
    input1.close();
    input1.clear();
    

    But I recommend instead you don’t reuse them. If you don’t want to have more than one variable around at once, you can keep each one in its own scope:

    int k,l;
    
    {
        std::ifstream input1("File1.txt");  
        input1 >> k;
    }
    {
        std::ifstream input1("File2.txt");  
        input1 >> l;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable
Possible Duplicate: Can I use a generated variable name in PHP? Am stuck here!
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: Can i save lots of bitmaps to one bitmap? (2d) I wonder
Possible Duplicate: Can you write object oriented code in C? I'm wondering if it's
Possible Duplicate: Can I use ASP.NET MVC together with regular ASP.NET Web forms Let's
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? Does it possible to get
Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
Possible Duplicate: Can I use a binary literal in C or C++? I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.