美文网首页
c++文件操作

c++文件操作

作者: arkliu | 来源:发表于2022-10-31 09:08 被阅读0次

创建和写入文本文件

#include<iostream>
#include<fstream> //ofstream类需要包含的头文件
#include<string>

using namespace std;

int main() {
    string filename = R"(D:\test\test.txt)"; // 原始字面量定义文件路径
    ofstream ofs;
    /**
     * 第二个参数打开模式,有三种:
     * 1. ios::out     默认值,会截断文件内容
     * 2. ios::trunc    截断文件内容
     * 3. ios::app      追加写入
    */
    ofs.open(filename, ios::out); // 打开文件,若文件不存在,则创建文件
    if (ofs.is_open() == false)
    {
        cout << "打开文件:"<<filename << "  失败了"<<endl;
    } else {
        ofs << "你好,我叫李雷\n";
        ofs << "我是韩梅梅\n";
        ofs << "和我在成都的街头走一走,哦哦哦哦哦哦\n";

        cout << "文件写入成功..." << endl;
    }
    ofs.close();
    return 0;
}

读取文本文件

#include<iostream>
#include<fstream> //ifstream类需要包含的头文件
#include<string>

using namespace std;

int main() {
    string filename = R"(D:\test\test.txt)"; // 原始字面量定义文件路径
    ifstream ifs;

    ifs.open(filename); // 打开文件,若文件不存在,则打开失败
    if (ifs.is_open() == false)
    {
        cout << "打开文件:"<<filename << "  失败了"<<endl;
    } else {
        // 第一种读取方式
        // string buffer;
        // while (getline(ifs, buffer)) // getline 需要#include<string>头文件
        // {
        //     cout << buffer << endl;
        // }

        cout << "==========" << endl;
        // 第二种读取文本文件方式
        string buffer;
        while (ifs >> buffer)
        {
            cout << buffer << endl;
        }
    }
    ifs.close();
    return 0;
}

写入二进制文件

#include<iostream>
#include<fstream> //ofstream类需要包含的头文件
#include<string>

using namespace std;

class Student
{
    public:
        char name[128];
        int age;
        double weight;
};

int main() {
    string filename = R"(D:\test\test)"; // 原始字面量定义文件路径
    ofstream ofs;
    /**
     * 第二个参数打开模式,有三种:
     * 1. ios::out     默认值,会截断文件内容
     * 2. ios::trunc    截断文件内容
     * 3. ios::app      追加写入
     * 4. ios::binary   二进制写入
    */
    ofs.open(filename, ios::app | ios::binary); // 打开文件,若文件不存在,则创建文件
    if (ofs.is_open() == false)
    {
        cout << "打开文件:"<<filename << "  失败了"<<endl;
    } else {
        Student stu = {"张三", 22, 50.8};
        ofs.write((const char *)&stu, sizeof(struct Student));

        Student stu1 = {"李四", 45, 66.5};
        ofs.write((const char *)&stu1, sizeof(struct Student));
        cout << "文件写入成功..." << endl;
    }
    ofs.close();
    return 0;
}
}

和写入文本文件区别是:

  1. 打开文件的时候,指定打开模式为ios::binary,表示以二进制写入
  2. 使用ofstream对象的write()方法写入

读取二进制文件

#include<iostream>
#include<fstream> //ifstream类需要包含的头文件
#include<string>
using namespace std;

class Student
{
    public:
        char name[128];
        int age;
        double weight;
};

int main() {
    string filename = R"(D:\test\test)"; // 原始字面量定义文件路径
    ifstream ifs;

    ifs.open(filename, ios::binary); // 打开文件,若文件不存在,则打开失败
    if (ifs.is_open() == false)
    {
        cout << "打开文件:"<< filename << "  失败了"<<endl;
    } else {
        Student stu;
        while (ifs.read((char *)&stu, sizeof(stu)))
        {
            cout << "name:"<<stu.name <<"  age:"<<stu.age<<"  weight:"<<stu.weight<<endl;
        }
    }
    ifs.close();
    return 0;
}

读取二进制文件和文本问价的区别:

  1. 打开文件使用ios::binary, 以二进制格式打开
  2. 使用ifstream 的read函数读取自定义结构的内容

fsstream类

fsstream类,既可以读文本/二进制文件,也可以写入文本/二进制文件
fsstream包含ifstream ofstream的读写方法

文件的随机读写

文件位置指针

文件位置指针指向当前文件的读/写位置

  • ifstream获取和设置文件指针位置
ifs.tellg();  // 获取文件指针位置
ifs.seekg(55);  // 移动文件指针位置到55字节
ifs.seekg(ios::end); // 把文件指针移动到结束位置
ifs.seekg(30, ios::end); // 把文件指针从开始位置,往后移动30个字节
ifs.seekg(-10, ios::cur); // 把文件指针从当前位置往前移动10个字节
ifs.seekg(10, ios::cur); // 把文件指针从当前位置往后移动10个字节
  • ofstream获取和设置文件指针位置
ofs.tellp(); 获取文件指针位置
ofs.seekp(30);  //把文件指针移动到30字节的位置
ofs.seekp(ios::beg); // 把文件指针移动到开始位置
ofs.seekp(ios::end); // 把文件指针移动到结束位置

文件缓冲区和流状态

文件缓冲区是系统预留的内存空间,用于存放输入或者输出的数据,在c++中,每打开一个文件,系统就会为它分配缓冲区,不同的流缓冲区是独立的。

默认:输出缓冲区满了, 才把数据写入磁盘

输出缓冲区

flush()   直接刷新缓冲区
endl       换行,然后刷新缓冲区
#include<iostream>
#include<fstream> //fstream类需要包含的头文件
#include<string>
#include<unistd.h>
using namespace std;

int main() {
    string filename = R"(D:\test\test.txt)"; // 原始字面量定义文件路径
    fstream fs;

    fs.open(filename); // 打开文件,若文件不存在,则打开失败
    fs << unitbuf; // 设置输出流的缓冲方式
    // fs << nounitbuf;  // 让fs回到缺省的缓冲方式  
    for (size_t i = 0; i < 1000; i++)
    {
        // fs << "hello world \n" << endl; // endl换行,然后刷新缓冲区
        fs << "hello world \n";
        // fs.flush(); 刷新缓冲区
        usleep(100000);
    }
    
    fs.close();
    return 0;
}

流状态

  • eof 当输入流操作达到文件末尾时候,将设置eof为1, 使用eof()成员函数获取eof标志位的状态
  • 无法诊断的失败破坏流, bad将被置为1, 使用bad()成员函数获取bad标志位的状态
  • fail 当输入流操作未能读取预期的字符时,将设置fail为1 ,使用fail()成员函数获取fail标志位的状态
    当上述三个流都为0时, good函数返回true
#include<iostream>
#include<fstream> //ifstream类需要包含的头文件
#include<string>

using namespace std;

int main() {
    string filename = R"(D:\test\test.txt)"; // 原始字面量定义文件路径
    ifstream ifs;

    ifs.open(filename); // 打开文件,若文件不存在,则打开失败
    if (ifs.is_open() == false)
    {
        cout << "打开文件:"<<filename << "  失败了"<<endl;
    } else {
        string buffer;
        while (true)
        {   
            ifs >> buffer;
            if (ifs.eof()) // 输入流指针达到文件末尾,eof位被置1
            {
                break;
            }
            cout << buffer << endl;
        }
    }
    ifs.close();
    return 0;
}

相关文章

  • c++文件操作详解

    c++文件操作详解 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由o...

  • 文件读写总结

    1. C++文件读写详解 1.1. 文件读写操作 使用方式 1.1.1. 打开文件 文件操作通过成员函数open(...

  • 2-ndk学习之c++基础篇(05)

    文件操作 首先是c语言读取文件:需要先添加头文件, 然后是c++读取文件:先添加头文件: 多线程入门 c++的多线...

  • C++文件操作

    说明 假定文件流对象为 fstream fs; 读写模式打开文件时,需要使用 ios::in | ios::ou...

  • C++ 文件操作

    C++ 文件和流 到目前为止,我们已经使用了 iostream 标准库,它提供了 cin 和 cout 方法分别用...

  • c++文件操作

  • 【C++】C++文件流操作

    文件操作是每门语言的必学项目,C++中同样如此,且C++中通过流(stream)这一形式进行输入输出、文件读写的控...

  • C++文件操作相关

    代码 说明 主函数中首先定义了string类型的文件名对象;然后创建了 fstream 的对象;随后调用open函...

  • c++ 文件操作大全

    转载(https://www.cnblogs.com/MrYuan/p/5383408.html) 基于C的文件操...

  • C++ 文件读写操作

    依赖gcc、 cmake 1. 代码编写 main.cpp 2. cmake 编写 CMakeLists.txt ...

网友评论

      本文标题:c++文件操作

      本文链接:https://www.haomeiwen.com/subject/vaqhtdtx.html