Home arrow C++ arrow File Handling and Streams in C++

File Handling and Streams in C++

Welcome to the fourth part of a ten-part series covering the use of streams with C++. Streams can save you a lot of time and effort that would otherwise be spent on trivial, tedious tasks. In this article, we'll show you how to use streams to improve the data flow of your applications.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
April 07, 2009
  1. · File Handling and Streams in C++
  2. · Basic Tasks
  3. · The Mode Flags
  4. · The Binary Flag

print this article

File Handling and Streams in C++
(Page 1 of 4 )

Writing more advanced code in C/C++ might not always be a trivial task. Due to this, you might have to run an application several times before you manage to eliminate all of its problems. Now with most  applications, some input is required at a basic level at least. The quantity of required input can turn out to be quite large, so typing inside the console each time is definitely a good way to waste your precious time.

However, programming in C++ should be about looking over the details and seeing the big picture instead of handling trivial tasks like typing in the same input repeatedly. The way to evade this kind of practice is to use input that will not be cleared after the console closes. Off course, we are talking about files, preferably in text format.

Throughout this article, you will learn how to achieve good data flow in both directions (in and out) with the help of streams. These are members of a greater collection of streams in the library section named Iostream.

I strongly recommend reading my introductory articles about streams that appeared here on the Developer Shed Network under the names Introduction to Streams and Iostream Library and Basic IO in C++ because these will provide you with a better understanding of streams if you are not yet familiar with them. However, it is your call if you want to read them or not. The articles in this series should be just as useful independent of each other. 

If you do want to know more about these topics, however, I advise you to search for my article about streams in general called Introduction to Streams or, for a little more detail about the structure of the library, see the article titled Iostream Library and Basic IO in C++ (this also treats the cin/cout streams).

The header where all of this is included is named <fstream>, so be sure to include it each time you want to use the library. Adding the using namespace std will make your work easier, and the readability of your code will increase. To start using them, however, like anything in C++, you need to declare its type.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials