C++基础 ----- 文件操作

写在前面

  1. C++中对文件操作需要包含头文件<fstream>

  2. 文件类型分为两种

    文本文件:文件以文本对ASCII码形式存储在计算机中
    二进制文件:文件以文本的二进制形式存储在计算机中

  3. 操作文件的三大操作

    ofstream:写操作
    ifstream:读操作
    fstream:读写操作



文本文件的读写操作流程

一、写文件

(1) 流程

  1. 包含头文件:<fstream>
  2. 创建输出流对象:ofstream ofs;
  3. 输出流对象调用open方法打开文件:ofs.open("文件路径", 打开方式)
  4. 输出数据:ofs << 数据;
  5. 关闭流:ofs.close();

(2) 测试

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

void outputToFile(string file, string content, int mode) {
    ofstream ofs;
    ofs.open(file, mode);
    ofs << content << endl;
    ofs.close();
}

int main() {
    outputToFile("hello","momo\nmomo\nmo",ios::out);
}
二、读文件

(1) 流程

  1. 包含头文件:<fstream>
  2. 创建输入流对象:ifstream ifs;
  3. 输入流对象调用open方法打开文件:ifs.open("文件路径", 打开方式)
  4. 四种读取数据的方式 (详见测试代码)
  5. 关闭流:ifs.close();

(2) 测试

#include <fstream>
#include<iostream>
#include<string>

using namespace std;

string readFile(string file, int mode) {
    ifstream ifs;
    ifs.open(file, mode);
    string s;
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return nullptr;
    }

//    第一种方法
//    char buf[1024] = {0};
//    while (ifs >> buf) {
//        s.append(buf);
//    }

//    第二种方法:每次读取一行数据到buf中
//    char buf[1024] = {0};
//    while (ifs.getline(buf,sizeof(buf))) {
//        s.append(buf);
//        s.append("\n");
//    }

//    第三种方法:每次读取一行数据到buf中
//    string buf;
//    while (getline(ifs,buf)) {
//        s.append(buf + "\n");
//    }

//    第四种方法:每次读取一个字符到c中
//    char c;
//    EOF   end of file
//    while ((c = ifs.get()) != EOF) {
//        s.push_back(c);
//    }
	ifs.close();
    return s;
}

int main() {
    cout << readFile("hello",ios::in) << endl;
}
三、关于文件的打开方式
打开方式说明
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate写入的初始位置为文件尾
ios::app以追加方式写入文件
ios::trunc如果文件存在则删除后重新创建
ios::binary二进制方式

注意:文件打开方式可以配合使用,利用|操作符,如:ios::out | ios app



二进制文件的读写操作

流程基本和上面一样,下面只展示测试代码

一、写文件
#include<iostream>
#include<string>
#include <fstream>
using namespace std;

class Person {
public:
    int age;
    char* name;
    Person(int age, char* name){
        this->age = age;
        this->name = name;
    }
};

int main() {
    ofstream ofs("Person.txt",ios::out | ios::binary);
    Person p (11,"张三");
    ofs.write((const char*) &p, sizeof(Person));
    ofs.close();
}
二、读文件
#include<iostream>
#include<string>
#include <fstream>

using namespace std;

class Person {
public:
    int age;
    char *name;

    Person(int age, char *name) {
        this->age = age;
        this->name = name;
    }

    Person() {}
};

int main() {

    ifstream ifs;
    ifs.open("Person.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return 0;
    }
    Person p;
    ifs.read((char*) &p, sizeof(Person));

    cout << "姓名:" << p.name << "\n" << "年龄:" << p.age << endl;
    ifs.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值