C++
  Home arrow C++ arrow Page 3 - File Handling and Streams in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

File Handling and Streams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-04-07

    Table of Contents:
  • File Handling and Streams in C++
  • Basic Tasks
  • The Mode Flags
  • The Binary Flag

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    File Handling and Streams in C++ - The Mode Flags


    (Page 3 of 4 )

    Each time you open a file via a stream, the open procedure and how the file behaves are all controlled via some internal flags of the fstream class. Even when you specify nothing clearly, the default values will be used. The file modes are just integer values essentially, so we can combine them with a bitwise operator.

    Here is a list of what's available and their effects on the file stream:


    => in - open for input

    => out - open for output

    => app - seek to the end before every write

    => ate -seek to the end immediately after the open

    => trunc - truncate an existing stream when opening it

    => binary - do the I/O procedures in binary mode (the stream will consist of ones and zeros)


    Of course, not all of them have the same effect on the three types of file streams, and some do not even have a specific sense. The trunk flag will eventually delete the previous data inside the file. Opening an ofstream file with just the out flag works essentially just as if you had declared the trunk flag also, as it would clear anything that had been there before.

    The solution, if you only want to append into the file, is to use the app flag with the ofstream. In the case of the fstream, the trunc is no longer there by default, so all of the previous data inside the file will remain.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek