newbie question reguarding safe practice of file io

This is a discussion on newbie question reguarding safe practice of file io within the C++ Programming forums, part of the General Programming Boards category; what im wondering about is can I read files, without worrying about destroying data? Below is a snippet out of ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    newbie question reguarding safe practice of file io

    what im wondering about is can I read files, without worrying about destroying data?

    Below is a snippet out of tutorial 10 file io, it says I have too be careful of destroying data, does this account for any type of stream? or is it just input stream that writes too the file?

    This will open the file without destroying the current contents and allow you to append new data. When opening files, be very careful not to use them if the file could not be opened. This can be tested for very easily:

    Code:
    ifstream a_file ( "example.txt" );
    
    if ( !a_file.is_open() ) {
      // The file could not be opened
    }
    else {
      // Safely use the file stream
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    It applies to ostreams, since by default, they will truncate an existing file.
    An istream will fail if the input file does not exist.

    "Safely" using them if it could not be opened is another thing entirely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The danger it's warning you about is that files opened for output only will erase the existing file contents unless explicitly told not to.

    This sounds like the issue is greatly overblown. You only need to do the thing that makes sense, not take extra care to avoid some special traps.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Newbie Question: File Input and Relative Paths
    By Ashes999 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2003, 05:21 AM
  5. Episode II Return Of the newbie : Website IO Question
    By MagiZedd in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 09:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21