Latest web development tutorials

C ++ files and streams

So far, we have used the standardiostream library, which provides cinandcoutstream methods are used to read from standard input and write to the standard output stream.

This tutorial shows you how to read and write files from stream to stream files. That's where another C ++ Standard Libraryfstream, it defines three new data types:

数据类型 描述
ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息。
ifstream 该数据类型表示输入文件流,用于从文件读取信息。
fstream 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。

To file handling in C ++, you must include the header file in C ++ source code file <iostream> and <fstream>.

open a file

Before reading the information from the file or write to a file, you must first open the file.ofstream and fstreamobjects can be used to open the file for writing, if you only need to open the file for reading, use theifstreamobject.

Here is the open () function is standard syntax, open () function is a member of fstream, ifstream and ofstream objects.

void open(const char *filename, ios::openmode mode);

Here, open () member function of the first parameter to specify the name and location of the file you want to open, and the second parameter defines the file is opened mode.

模式标志 描述
ios::app 追加模式。所有写入都追加到文件末尾。
ios::ate 文件打开后定位到文件末尾。
ios::in 打开文件用于读取。
ios::out 打开文件用于写入。
ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。

You can use a combination of two or more of the above modes. For example, if you want to open the file in write mode and want to truncate the file to prevent the file already exists, you can use the following syntax:

ofstream outfile;"file.dat", ios::out | ios::trunc );

Similarly, you if you want to open a file for reading and writing, you can use the following syntax:

fstream  afile;"file.dat", ios::out | ios::in );

Close the file

When the C ++ program terminates, it will turn off automatically refresh all streams, release all allocated memory and close all open files. But programmers should develop a good habit, before the program terminates close all open files.

Here is the close () function is standard syntax, close () function is a member of fstream, ifstream and ofstream objects.

void close();

Write to file

In the C ++ programming, we use the stream insertion operator (<<) to write information to a file, just like using the output information to the operator on the same screen. The only difference is that here you are usingofstream or fstreamobject instead ofcoutobject.

Read the file

In the C ++ programming, we use the stream extraction operator (>>) to read information from a file, just like using the operator input information from the keyboard. The only difference is that here you are usingfstream or ifstreamobject instead ofcinobject.

Read & write examples

The following C ++ program to open a file in read-write mode. After the write to the file afile.dat information entered by the user, the program reads the information from the file and output to the screen:

#include <fstream>
#include <iostream>
using namespace std;
int main ()
   char data[100];

   // 以写模式打开文件
   ofstream outfile;"afile.dat");

   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 100);

   // 向文件写入用户输入的数据
   outfile << data << endl;

   cout << "Enter your age: "; 
   cin >> data;
   // 再次向文件写入用户输入的数据
   outfile << data << endl;

   // 关闭打开的文件

   // 以读模式打开文件
   ifstream infile;"afile.dat"); 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // 在屏幕上写入数据
   cout << data << endl;
   // 再次从文件读取数据,并显示它
   infile >> data; 
   cout << data << endl; 

   // 关闭打开的文件

   return 0;

When the above code is compiled and executed, it produces the following inputs and outputs:

Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file

The above examples use an additional function cin object, such as getline () function reads a line from the outside, ignore () function will read the statement before leaving out the extra characters are ignored.

File position pointer

istream and ostreamprovide member functions for repositioning the file pointer position. These include about istream member functions ofseekg ( "seek get") and on the ostream seekp ( "seek put").

seekg seekp and the argument is usually a long integer. The second parameter allows you to specify the direction of search. Find direction can beios :: beg (default, positioning from the beginning of the stream), it can be ios :: cur (positioningfrom the current position of the stream), it can beios :: end (fromthe end of the stream is started positioning).

File position pointer is an integer value that specifies the number of bytes from the beginning of the file to the location of the pointer. Here is targeting "get" file position pointer instance:

// 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
fileObject.seekg( n );

// 把文件的读指针从 fileObject 当前位置向后移 n 个字节
fileObject.seekg( n, ios::cur );

// 把文件的读指针从 fileObject 末尾往回移 n 个字节
fileObject.seekg( n, ios::end );

// 定位到 fileObject 的末尾
fileObject.seekg( 0, ios::end );