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 1084331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:32:54+00:00 2026-05-16T22:32:54+00:00

I have ifstream and an ofstream that in runtime might be opened or not

  • 0

I have ifstream and an ofstream that in runtime might be opened or not (depends on what the user enters in command line. i declare the variables anyway, and i have a method that opens the stream if needed.
my problem is at the end of the program i don’t know if i need to close them or not.

Is there anyway in c++ to know if a stream was opened? Like in Java you can give a stream the null value and then ask if its null (it means that it was never opened)..

Is it ok to close a stream that was never opened?

this is the code:

int main(int argc, char* argv[]) {

    static std::ifstream ifs;
    static std::ofstream ofs;

        //might or might not open the streams:
    OpenStreams(ifs,ofs,argc-1,argv);
        ........

        //here i would like to close the streams at the end of the program
        //or not (if they were not opened

    return 0;

}

Thanks!

  • 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-16T22:32:54+00:00Added an answer on May 16, 2026 at 10:32 pm

    No close call needed – the streams close itself when they are open when they are destroyed. Also, the static there looks suspicious. main is called only once, so it doesn’t have any effect here (apart from pedantic standardese differences that don’t matter here, i think…. Definitely not in the case shown).

    That said, you can just call close if a stream is not opened – close will return a null pointer if it wasn’t open. (I was looking at the spec for basic_filebuf<>::close – the file streams’s close returns void).

    File-streams can also handle non-open streams: If the stream wasn’t open, it sets the failbit. You can check for that using fail() (which tests whehter the failbit or badbit is set). But there is is_open anyway to test whether the stream is open, but you don’t need it for the above reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the next code: std::string line; std::ifstream myfile (text.txt); if (myfile.is_open()) { while
Newbie C++ programmer here. I'm trying to write a command line application that takes
I have the following code: ifstream initFile; initFile.open(D:\\InitTLM.csv); if(initFile.is_open()) { // Process file }
I have been having trouble with opening files with ifstream if I do: ifstream
I'm relatively new to c++, and have some issues with ifstream. All I want
#include <iostream> #include <fstream> using namespace std; int main() { ifstream stream1(source.txt); string line
I am working on a Windows VC++2008 program that does fileIO, and have hit
I have a function that takes an ostream reference as an argument, writes some
I understand no copy or assign for IO objects, so that we have to
I have code that manipulates binary files using fstream with the binary flag set

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.