<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 12 File Input and Output_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 12: File Input and Output
 More of this Feature
• File I/O
• File I/O, continued
• Methods of I/O Classes
• Methods of I/O Classes, continued
• I/O Manipulators
• I/O Manipulators, continued
• Solution to Practice Problem
 
 Related Resources
• C++ Tutorial - Lesson 4: Input and Output
• C++ Programming Tips - Tips on Input
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's tutorial on C++ programming. This lesson covers file input and output, and iostream manipulators. Learning how to read and write files is an important step in learning any programming language. Any real world application is likely to process large amounts of information. A simple technique to transfer and record data is via files. More advanced techniques for data storage and manipulation can involve the use of relational databases or special data formats such as XML. For now, we will study the use of text files.

One important issue with writing results to output files is data format. Whether the ultimate consumer of a programs output be man or machine, the format of this output can be as significant as its content. If the output is being consumed by another program, then the output format is likely to be predetermined and highly specific in order for the consuming program to be able to properly read and access the data. If the output file is to be read by people, data format can significantly influence understanding and utility. Iostream manipulators provide a way to control output format.

File Input and Output
The techniques for file input and output, i/o, in C++ are virtually identical to those introduced in earlier lessons for writing and reading to the standard output devices, the screen and keyboard. To perform file input and output the include file fstream must be used.

#include <fstream>

Fstream contains class definitions for classes used in file i/o. Within a program needing file i/o, for each output file required, an object of class ofstream is instantiated. For each input file required, an object of class ifstream is instantiated. The ofstream object is used exactly as the cout object for standard output is used. The ifstream object is used exactly as the cin object for standard input is used. This is best understood by studying an example.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream myFile("c:/out.txt");
        // Creates an ofstream object named myFile

    if (! myFile) // Always test file open
    {
        cout << "Error opening output file" << endl;
        return -1;
    }

    myFile << "Hello World" << endl;

    myFile.close();

    return 0;
}

Let's examine this program. The first step created an ofstream object named myFile.

The constructor for the ofstream class takes two arguments. The first specifies a file name as a C-style string, the second a file mode. There are two common file open modes, truncate and append. By default, if no mode is specified and the file exists, it is truncated. The mode is specified by using an enumerator define in the ios class. The ios class contains members which describe modes and states for the input and output classes. An enumeration is a way to define a series of constants. Please see the lesson on constants for more detail. Fortunately, all this boils down to something very simple.






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.