文件和流

iostream标准库提供了cin和cout方法用于标准输入读取流和向标准输出写入流。

从文件读取流和向文件写入流,需要用到fstream库。它定了三个数据类型

数据类型

描述

ofstream

该数据类型表示输出文件流,用于创建文件并向文件写入信息

ifstream

该数据类型表示输入文件流,用于从文件读取信息

fstream

该数据类型通常表示文件流,同时具有ofstream和ifstream两种功能,这意味着它可以创建文件,从文件中读取信息,向文件写入信息

在C++中进行文件处理,必须在C++源代码文件中包含头文件<iostream>和<fstream>

打开文件

在从文件读取信息或向文件写入信息之前,必须先打开文件。ofstream和fstream对象都可以用来打开文件进行写操作,如果只需要打开文件进行对文件,则使用ifstream对象。

下面是open()函数的标准语法,open()函数是fstream、ifstream、ofstream对象的一个成员。

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

在这里open()成员函数的第一个参数指定要打开的文件的名称和位置,第二个参数定义文件被打开的模式。

模式标志

描述

ios::app

追加模式,文件写入都追加到文件末尾

ios::ate

文件打开后定位到文件末尾

ios::in

打开文件用于读取

ios::out

打开文件用于写入

ios::trunc

如果该文件已经存在,其内容将在打开文件之前被阶段,即把文件长度设置为0

上面两种或两种以上的模式结合使用。

例如想要以写入模式打开文件,并希望以写入模式打开文件,并希望截断文件,以防文件已存在,那么可以说使用以下语法:

ofstream outline;

outline.open(“file.dat”,ios::out | ios::trunc);

如果想要打开一个文件用于读写,可以使用以下语法:

ifstream afile;

afile.open(“file.dat”,ios::out | ios:: in);

关闭文件

当C++程序终止时,它会自动关闭刷新所有流,释放所有分配的内存,并关闭所有打开的文件。但程序员应该养成一个好习惯,在程序终止前关闭所有打开的文件。

close()函数时fstream、ifstream和ofstream对象的一个成员

void close();

写入文件

在C++编程中,我们使用流插入符(<<) 向文件写入信息,就像使用该运算符输出信息到屏幕上一样。唯一不同的是,在这里使用的是ofstream或fstream对象,而不是cout对象。

读取文件

在C++编程中,我们使用流插入符(>>) 向文件读取信息,就像使用该运算符输出信息到屏幕上一样。唯一不同的是,在这里使用的是ofstream或fstream对象,而不是cin对象。

/***
afile.cpp
***/
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    char data[100];

    //open file with write mode
    ofstream outfile;
    outfile.open("afile.dat");

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

    //write data of user inout to file
    outfile << data << endl;

    cout << "Enter you age: ";
    cin >> data;
    cin.ignore();

    //write data to file again
    outfile << data << endl;

    //close the file
    outfile.close();

    //open file with read mode
    ifstream infile;
    infile.open("afile.dat");

    cout << "Reading from the file " << endl;
    infile >> data;

    //write data in screen
    cout << data << endl;

    //read data from file again and show it in screen
    infile >> data;
    cout << data << endl;

    //close the reading file
    infile.close();

    return 0;
}

运行结果:

exbot@ubuntu:~/wangqinghe/C++/20190813$ g++ afile.cpp -o afile

exbot@ubuntu:~/wangqinghe/C++/20190813$ ./afile

Write to the file

Enter your name: wangqinghe

Enter you age: 25

Reading from the file

wangqinghe

25

上面的实例使用了cin对象的附件函数,比如getline()从外部读取一行,ignore()函数会忽略掉之前读语句留下的多余字符。

文件位置指针

istream和ostream都提供了用于重新定位文件位置指针的成员函数。这些成员函数包括istream的seekg(“seek get”)和关于ostream的seekp(“seek put”)。

seekg和seekp的参数通常是一个长整型,第二个参数可以用于指定查找方向。查找方向可以是iOS::beg(默认从流的开头开始定位),也可以是ios::cur(从流的当前位置开始定位),也可以是ios::end(从流的末尾开始定位)。

文件位置指针是一个整数值,指定了从文件起始位置到文件所在位置的字节数。

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

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

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

//定位到问价末尾
fileObject.seekg(0,ios::end);
原文地址:https://www.cnblogs.com/wanghao-boke/p/11343772.html