• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C++极快入门11文件操作

武飞扬头像
hello world 999
帮助1

文件操作


程序运行时产生的数据都属于临时的数据,随着程序运行的结束都会被释放,可以通过文件将数据持久化。

  1. cpp中对文件操作需要包含头文件<fstream>
  2. 文件类型分为两种:文本文件(以文本的ASCII码形式存储)、二进制文件(以二进制形式存储)
  3. 操作文件的文件流分为三类:ofstream写操作、ifstream读操作、fstream读写操作

一、文本文件:

1.指定打开方式:
打开方式 说明
ios::in 只读
ios::out 只写
ios::ate 初始位置:文件尾
ios::app 追加
ios::trunc 若文件存在先删除,再创建
ios::binary 二进制方式

注意:文件的打开方式可以相互组合,利用|实现(例如:用二进制方式写文件为ios::binary | ios::out

2.文本文件的写操作:
#include <fstream>//step1:包含头文件
...
ofstream ofs;//step2:创建流对象
...
ofs.open("文件路径", 打开方式)//step3:打开文件
...
ofs << "写入的数据";//step4:向文件中写入数据
...
ofs.close();//step5:关闭文件
#include <iostream>
#include <fstream>
using namespace std;

//1.文本文件写文件
void test01(){
    //(1)创建流对象
    ofstream ofs;
    //(2)指定打开方式
    ofs.open("test.txt", ios::out);
    //(3)向文件中写入数据
    ofs << "author:luochenhao" << endl;
    ofs << "gender:male" << endl;
    ofs << "age:21" << endl;
    //(4)关闭文件
    ofs.close();
}

int main(){
    test01();

    system("pause");
    return 0;
}
学新通

学新通

在当前目录下创建了一个名为test.txt的文件,进行了向其中写入一些数据的操作。

3.文本文件的读操作:
#include <fstream>//step1:包含头文件
...
ifstream ifs;//step2:创建流对象
...
ifs.open("文件路径", 打开方式)//step3:打开文件,并判断文件是否成功打开
...
Data can be read in four ways//step4:从文件中读取数据
...
ifs.close();//step5:关闭文件 
#include <iostream>
#include <fstream>
using namespace std;

//2.文本文件读文件
void test02(){
    //(1)创建流对象
    ifstream ifs;
    //(2)打开文件,并判断是否打开成功
    ifs.open("test.txt", ios::in);
    if(!ifs.is_open()){
        cout << "文件打开失败" << endl;
        return;
    }
    //(3)从文件中读取数据
    cout << "方式1:利用while循环将文件中的数据全部放在buf字符数组中" << endl;
    char buf1[1024] = {0};
    while(ifs >> buf1) {
        cout << buf1 << endl;
    }

    cout << "方式2:利用ifs的成员函数getline一行行将文件中的数据全部放在buf字符数组中" << endl;
    char buf2[1024] = {0};
    while(ifs.getline(buf2, sizeof(buf2))) {
        cout << buf2 << endl;
    }

    cout << "方式3:利用全局函数getline一行行将文件中的数据全部放在的buf字符串中" << endl;
    string buf3;
    while(getline(ifs, buf3)){
        cout << buf3 << endl;
    }

    cout << "方式4:利用ifs的成员函数get从文件中一个个字符进行读取" << endl;
    char c;
    while((c = ifs.get()) != EOF) {
        cout << c;
    }
    //(4)关闭文件
    ifs.close();
}

int main(){
    test02();

    system("pause");
    return 0;
}
学新通

学新通

从当前目录下读取刚才新创建的test.txt文件,将信息输出在屏幕上。

疑问:4种读取数据的方式都可行,为什么只有一次文件结果的输出?

二、二进制文件:

以二进制的方式对文件进行读写操作,打开方式需要指定为ios::binary

1.二进制文件的写操作:

二进制方式写文件主要利用流对象调用成员函数write

函数原型:ostream& write(const char *buffer, int len);

参数解释:字符指针buffer指向内存中一段存储空间(数据地址),len是读写的字节数

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

//1.二进制文件写文件
class Person{
public:
    char name[64];
    int age;
};

void test01(){
    //(1)创建流对象
    ofstream ofs;
    //(2)指定打开方式
    ofs.open("person.txt", ios::out|ios::binary);
    //(3)向文件中写入数据
    Person p1 = {"luochenhao", 21};
    ofs.write((const char *)&p1, sizeof(Person));
    //(4)关闭文件
    ofs.close();
}

int main(){
    test01();

    system("pause");
    return 0;
}
学新通

学新通

在当前目录下创建了一个名为person.txt的文件,进行了向其中以二进制的方式写入一些数据的操作。

注意:由于数据是以二进制的方式写入,直接打开person.txt文件后发现有些数据无法正常显示属于正常。

2.二进制文件的读操作:

二进制方式读文件主要利用流对象调用成员函数read

函数原型:istream& read(char *buffer, int len);

参数解释:字符指针buffer指向内存中一段存储空间(数据地址),len是读写的字节数

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

//2.二进制文件读文件
class Person{
public:
    char name[64];
    int age;
};

void test02(){
    //(1)创建流对象
    ifstream ifs;
    //(2)打开文件,并判断是否打开成功
    ifs.open("person.txt", ios::in|ios::binary);
    if(!ifs.is_open()){
        cout << "文件打开失败!" << endl;
        return;
    }
    //(3)从文件中读取数据
    Person p2;
    ifs.read((char *)&p2, sizeof(Person));
    cout << "姓名:" << p2.name << endl;
    cout << "年龄:" << p2.age << endl;
    //(4)关闭文件
    ifs.close();
}

int main(){
    test02();

    system("pause");
    return 0;
}
学新通

学新通

从当前目录下读取刚才新创建的person.txt文件,将信息输出在屏幕上。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhifkjfa
系列文章
更多 icon
同类精品
更多 icon
继续加载