Loading

C++文件的读写

文件读写函数库

对于文件对象的操作,主要使用库:#include <fstream>

类可以定义三种类对象:

  • ifstream 定义的对象只能读文件
  • ofstream 定义的对象只能写文件
  • iofstream 定义对象既能读文件,也能写文件

类定义的对象中open()方法的第二个参数文件模式(file mode)有多种属性:

  • in : 以读方式打开
  • out : 以写的方式打开
  • app : 每次写操作前均将写指针移动到文件末尾
  • ate : 打开文件后立即将文件指针移动到末尾
  • trunc : 截断文件
  • binary : 以二进制方式操作IO

📌 每次open()后,读写操作完毕,最后一定记住close()

读写指针的移动操作方法:

  • tellg() : 用于读指针,返回位置

  • seekg() : 用于读指针,移动位置

  • tellp() : 用于写指针,返回位置

  • seekp() : 用于写指针,移动位置

移动操作方法有两个参数:int offset and int mode。其中mode有如下几种可选:

  • ios::beg : 该模式让读(写)指针从文件开始处向文件末尾处移动offset个单位。若offset == 0,即移动至文件开头位置。要求offset >= 0
  • ios::cur : 该模式让读(写)指针从当前所在处移动offset个单位。若offset < 0,即往文件开头方向移动|offset|位置。若offset > 0,即往文件末尾方向移动offset
  • ios::end : 该模式让读(写)指针从文件末尾处向文件开始处移动|offset|个单位。要求offset <= 0

只读操作

#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
    /* file read-only */ 
    const char *file_path = "file.txt";
    ifstream ifile;
    vector <string> vec_str;
    string str;
    vector <string>::const_iterator iter_str;

    cout << "read file-only:" << file_path << endl;
    ifile.open(file_path, ios::in);
    if (!ifile)
    {
        cout << "open file fail." << endl;
        exit(1);
    }
    // 逐行读取并存储到vector<string>
    while (!ifile.eof())
    {
        getline(ifile, str);
        vec_str.push_back(str);
    }
    // 迭代器遍历 vector<string> 并打印其值
    for (iter_str = vec_str.begin(); iter_str != vec_str.end(); iter_str++)
    {
        cout << *iter_str << endl;
    }
    ifile.close();  

    exit(0);
}


写操作

#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
    /* file write-only */ 
    const char *file_path = "test.txt";
    ofstream ofile;
    char str[1024] = {0};
    vector<string> vec_str;

    ofile.open(file_path, ios::out);
    if (!ofile)
    {
        cout << "open file fail." << endl;
        exit(1);
    }
    for (int i = 0; i < 4; i++)
    {
        snprintf(str, sizeof(str), "the line number:%d\n", i + 1);
        vec_str.push_back(str);
    }
    // 遍历 vector<string> 并写入ofile
    for (int i = 0; i < vec_str.size(); i++)
    {
        ofile << vec_str[i]; 
    }
    vec_str.clear(); // 清空vector<string>的内容
    ofile.close();   

    exit(0);
}

读写操作

#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
    /* file read-write */ 
    fstream iofile;
    const  char *file_path = "test.txt";
    char buffer[128] = {0};

    iofile.open(file_path, ios::in | ios::out);
    if (!iofile)
    {
        cout << "open file fail." << endl;
        exit(1);
    }

    iofile.seekp(0, ios::end); // 将写指针移动到文件末尾
    int cur_w = iofile.tellp(); // 获取写指针位置
    cout << cur_w << endl;

    snprintf(buffer, sizeof(buffer), "the line number:5\n");
    iofile.write(buffer, strlen(buffer));

    iofile.seekg(0, ios::beg); // 将读指针移动到文件开头
    int cur_r = iofile.tellg(); // 获取读指针位置
    cout << cur_r << endl;

    while (!iofile.eof())
    {
        iofile.read(buffer, sizeof(buffer) - 1);
        cout << buffer;
    }
    iofile.close();

    exit(0);
}

程序运行之前:

程序运行之后:

📌 当 while (!iofile.eof()) 结束后,我尝试将写指针 或 读指针 移动位置,最后发现无论怎么操作读写指针都等于-1

posted @ 2023-09-20 10:01  eiSouthBoy  阅读(147)  评论(0)    收藏  举报