How to Use <fstream> Classes for File I/O
By Danny Kalev, C++ Pro

Traditional file I/O libraries such as Unix <io.h> and <stdio.h> impose a significant burden on the programmer due to their procedural interface, lack of type-safety, lack of internationalization support. The C++ <fstream> library provides an enhanced, object-oriented, internationalization-aware library for file I/O. This 10-Minute Solution will show you how to use this library to facilitate file I/O operations and develop cross-platform code easier.

Most C++ programmers are familiar with two to four different file I/O libraries. First, there is the traditional Unix-style library that consists of low-level functions such as read() and open(). Then there is the ANSI C <stdio.h> library that contains the fopen() and fread() functions. Often, there's a proprietary library or framework, say MFC, with its file-related classes.



The propensity of these libraries makes cross-platform development difficult. Worse yet, the aforementioned C libraries impose a significant burden on the programmer due to their procedural interface and lack of type-safety.



Standard C++ provides an enhanced, object-oriented, internationalization-aware library for file I/O, namely <fstream>. This library contains a family of class templates derived from the standard ios_base and ios classes. Consequently, <fstream> offers a high level of automation and robustness. In the following sections, I will show how to use <fstream> classes.

  
Next: Creating a Filestream

Introduction
Creating a Filestream
Setting a File's Position

All DevX 10-Minute Solutions for C/C++

Return to Get Help with C/C++ Page

Return to Main Get Help Page


 


Find Out More

Microsoft on <fstream>

Use of <fstream>

C++ Resources: <fstream>

Tech Tip: Recent Changes in the Specification of Open Mode Flags

Tech Tip: Copying Files with File Streams


TALK BACK
How important is a compiler's standard compliance to you? What is your topmost priority for choosing a compiler or IDE?


Sponsored Links