C++文件的读取操作

时间:2025-04-15 07:17:06

1.文件操作常用到的类是fstream类。

fstreamshi菱形继承类所以他包括的ifstream类和ofstream类。

ifstream定义读文件的对象。

ofstream定义写文件的对象。

:文件的打开方式。

open打开文件这个函数有两个参数:第一个是我们打开文件的名,第二个是打开得方式。

1)打开文件的方式有:

app:以可写的方式打开文件并且是在文件内容的末尾进行写入,不会覆盖掉之前文件的内容。

binary:是以二进制的方式打开文件。

in:是以只读的方式打开文件不能改变文件的内容。

out:打开文件进行写的操作,即是把数据写入文件中。

ate:打开文件是文件指针指向文件的末尾,但是可以在任意的位置进行插入。

trunc:如果打开的文件已经存在则清空文件的内容。

下面是具体的示例:

```

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

//对文件进行写入的操作
void write()
{
    ofstream file;
    ("./", ios_base::app);

    if (!file.is_open())
    {
        cout << "打开文件失败";
    }

    string ss;
    cin >> ss;
    file << ss << endl;

    ();
}

//第一种读取文件的操作
void read1()
{
    ifstream file;

    ("./", ios_base::in);

    if (!file.is_open())
    {
        cout << "打开文件失败";
    }

    string s;
    while (getline(file, s))
    {
        cout << s << endl;
    }
    ();
}

//第二种读取文件的操作
void read2()
{
    ifstream file;

    ("./", ios_base::in);

    if (!file.is_open())
    {
        cout << "打开失败";
    }

    stringstream s;
    s << ();

    string str = ();
    cout << str << endl;

}

int main()
{
    write();
    read1();
    cout << endl;
    read2();
    cout << endl;
    return 0;
}
 

```