1.流类型。
| 数据类型 | 描述 | 
|---|---|
| ofstream | 输出文件流,o表示outPut | 
| ifstream | 输入文件流,i表示inPut | 
| fstream | 输入,输出文件流具备输入和输出的能力 | 
2.文件打开。
 void open(const char *filename, ios::openmode mode); 
3.文件打开模式。
| 模式标志 | 描述 | 
|---|---|
| ios::app | 追加模式,将文件内容追加到文件底部 | 
| ios::ate | 文件打开后定位到文件末尾。 | 
| ios::in | 打开文件用于读取。 | 
| ios::out | 打开文件用于写入。 | 
| ios::trunc | 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。 | 
4.语法示例
 ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc ); 
注意可以使用|同时将两个模式进行并用。
5.写入文件
 流对象<< data << endl; 
6.读取文件
 文件流对象 >> data; 
7.关闭文件。
 void close(); 
8.文件写入代码。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
    char str[100];
    ofstream outfile;
    outfile.open("dfile.txt");
    cout << "input text:" << endl;
    cin.getline(str, 100);
    cin.ignore();
    outfile << str << endl;
    outfile.close();
    return 0;
}9.文件读取代码。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
    char str[100];
    ifstream infile;
    infile.open("dfile.txt");
    infile >> str;
    cout << str << endl;
    infile.close();
    return 0;
}
10.seekp跳过多少个字节。
// 定位到 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 );类似于Java流中的skip,但Java中只能从0开始往后跳。
 
        					