CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    50

    Why should I close a file?

    Hi,

    I building an app that write info to a file every few seconds using:
    Code:
    std::wofstream FileWrite
    At the moment I am closing the file after writing each info message,
    And I was wondering if I am the only one who is writing to the file why should i close it?

    Can I leave a file open during the application life time?

    Regards

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Why should I close a file?

    No reason why not. Probably better to do so, actually.

  3. #3
    Join Date
    Jul 2008
    Posts
    50

    Red face Re: Why should I close a file?

    Thats what everyone say, but I still can't get a clear answer explaining why not.
    Last edited by PeterRoddis; July 29th, 2008 at 06:22 PM.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why should I close a file?

    Quote Originally Posted by PeterRoddis
    Thats what everyone say, but I still can't get a clear answer explaining why not.
    That's because there is no reason why not.

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Why should I close a file?

    Quote Originally Posted by PeterRoddis
    Hi,

    I building an app that write info to a file every few seconds using:
    Code:
    std::wofstream FileWrite
    At the moment I am closing the file after writing each info message,
    And I was wondering if I am the only one who is writing to the file why should i close it?

    Can I leave a file open during the application life time?

    Regards
    I think there is at least one good reason to close the file: If the app crashes, you don't loose any data which may be hanging around in the output buffer.

    A smaller concern is the general rule of not holding on to resources unnecessarily.
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Jul 2008
    Posts
    50

    Re: Why should I close a file?

    Quote Originally Posted by Zaccheus
    I think there is at least one good reason to close the file: If the app crashes, you don't loose any data which may be hanging around in the output buffer.

    A smaller concern is the general rule of not holding on to resources unnecessarily.
    I thought that flushing the buffer would solve this issue.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Why should I close a file?

    Quote Originally Posted by PeterRoddis
    I thought that flushing the buffer would solve this issue.
    It would.

  8. #8
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Why should I close a file?

    Hi PeterRoddis,

    one reason I can think of is when you want to associate the same fstream object with another file.

    Code:
    fstream fout;
    fout.open("abc.txt"); // ok
    fout.open("def.txt") // Error. fstream object must be closed to open a new file
    was this so obvious that it was uncessary for me to even bring it up??
    sorry I'm still learning.

  9. #9
    Join Date
    Jul 2008
    Posts
    50

    Re: Why should I close a file?

    Quote Originally Posted by potatoCode
    Hi PeterRoddis,

    one reason I can think of is when you want to associate the same fstream object with another file.

    Code:
    fstream fout;
    fout.open("abc.txt"); // ok
    fout.open("def.txt") // Error. fstream object must be closed to open a new file
    was this so obvious that it was uncessary for me to even bring it up??
    sorry I'm still learning.

    It is a good point 8-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured