3 Replies - 1573 Views - Last Post: 01 February 2010 - 09:29 AM Rate Topic: -----

#1 pingking123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 19-November 09

fstream::close()

Posted 01 February 2010 - 06:23 AM

Suppose you open an fstream object:
std::fstream fs;
fs.open("C:\Exp.txt",std::ios_base::in);
//....


But you never use 'fs.close()'. What happence? Is that being done, if needed, on the destructor of 'fs'? If not than what exactly happence if the fstream object is never closed? Does it realy matter?
Is This A Good Question/Topic? 0
  • +

Replies To: fstream::close()

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: fstream::close()

Posted 01 February 2010 - 06:34 AM

YES, yes, .close() is not here, just to be used sometimes ... I'm gonna give you an example, in which if you don't call .close(), your program will crash:

int main()
{
  std::ifstream if;
  if.open("test.txt"); // open test.txt
  // do whatever reading we want from test.txt
  ...
  // if.close(); // uncomment this line, and the program will work!
  // now we want to write something to "test.txt"
  std::ofstream of; // open an already opened file, will fail!!
  of.open("test.txt"); // file already opened, see lines above 
  of << "this line won't be written, probably runtime error before getting here!!" << std::endl;

  return 0;


This post has been edited by sarmanu: 01 February 2010 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: fstream::close()

Posted 01 February 2010 - 07:01 AM

If you don't call it without using it again, your OS will clean up after you. If you do try to open up another stream or reopen the same stream it can fail silently (this seems to be the case more often then naught in my experience)or fail spectacularly (I like these more :) ).

It's good practice to close() whatever you open(), just like new/delete
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: fstream::close()

Posted 01 February 2010 - 09:29 AM

All things going well the stream's deconstructor will close the file... (actually it is the filebuf's deconstructor that actually does the closing).

The OS *may* also clean up any file handles that were not closed. However there do seem to be situations where the OS assumes that the file was left open for a reason... or perhaps it just can not close the file for some reason.

So it is really the best practice to close a file when you are done with it. If you forget the computer will not burst into flames -- but it may cause a file to be locked until the computer is rebooted -- and it may cause the disk to be come currupted.... the latter two are unlikely -- but possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1