C++文件操作

文章目录

零、文件操作的概念

一、文本文件

1.写文本文件

1.文件打开方式

2.步骤

3.代码

cpp 复制代码
//文本文件 : 写文件
#include <iostream>
#include <fstream> //1.包含头文件
using namespace std;

int main(){
    //2.创建流对象
    ofstream ofs;
    //3.指定打开方式 : 写文件
    ofs.open("text.txt",ios::out);
    //4.写内容
    ofs << "HelloWorld!" << endl;
    ofs << "I'm Edward! A great programmer in the future!" << endl;
    //5.关闭文件
    ofs.close();

    return 0;
}

2.读文本文件 (4种方法)

1.步骤

2.四种读文件方式

(1)字符数组

cpp 复制代码
char buf[1024] = {0};
while(ifs >> buf){
    cout << buf << endl;  //每个单词作为一行读进程序
}

(2)getline

cpp 复制代码
char buf[1024] = {0};
while(ifs.getline(buf,sizeof(buf))){
    cout << buf << endl;
}

(3)string + getline

cpp 复制代码
string buf;
while(getline(ifs,buf)){
    cout << buf << endl;
}

(4)单个字符读取,效率低,不推荐

cpp 复制代码
char c;
while( (c = ifs.get()) != EOF){
    cout << c;
}

3.完整代码

注意先将上一节写好的文件,复制到指定位置才能读取文件

cpp 复制代码
//文本文件 : 读文件
#include <iostream>
#include <fstream>   //1.包含头文件
#include <string>
using namespace std;

int main(){
    //2.创建流对象
    ifstream ifs;
    //3.打开文件,并判断是否打开成功
    ifs.open("text.txt",ios::in);
    if(!ifs.is_open()){
        cout << "文件打开失败" << endl;
        return 0;
    }
    //4.读数据 (四种方式)
     //①第一种
//    char buf[1024] = {0};
//    while(ifs >> buf){
//        cout << buf << endl;  //每个单词作为一行读进程序
//    }
     //②第二种
//    char buf[1024] = {0};
//    while(ifs.getline(buf,sizeof(buf))){
//        cout << buf << endl;
//    }
     //③第三种
//    string buf;
//    while(getline(ifs,buf)){
//        cout << buf << endl;
//    }
     //④第四种:一个字符一个字符读,效率低
    char c;
    while( (c = ifs.get()) != EOF){
        cout << c;
    }
    //5.关闭文件
    ifs.close();
    return 0;
}

4.总结

二、二进制文件

1.写二进制文件

1.关键步骤

cpp 复制代码
ofs.write( (const char *)&p, sizeof(p));

2.完整代码:

运行成功后,自己到程序所在同级目录找到,写好的文件。

cpp 复制代码
//写二进制文件
#include <iostream>
#include <fstream>  //1.包含头文件
using namespace std;

class Person{
public:
    char m_Name[64];
    int m_Age;
};

int main(){
    //2.创建流对象
    ofstream ofs;
    //3.打开文件
    ofs.open("person.txt",ios::out | ios::binary); //二进制方式写文件
    //4.写文件
    Person p = {"Edward",25};
    ofs.write( (const char *)&p, sizeof(Person)); //Person * 强转为 const char *
    //5.关闭文件
    ofs.close();
    return 0;
}

3.补充

②创建流对象 + ③打开文件,可以合并为一步:

cpp 复制代码
ofstream ofs("person.txt",ios::out | ios::binary);

4.举例:把pdf文件的数据 用二进制方式 写入内存缓冲区buffer

ofstream对象命名为outfile,创建流对象和打开文件合并为一步

cpp 复制代码
#include <fstream>
using namespace std;

ofstream outfile("/home/edward/YR/out.pdf", ios::binary);
outfile.write( buffer, length );

2.读二进制文件

1.关键步骤

cpp 复制代码
ifs.read( (char *)&p, sizeof(p));

2.完整代码

同样,需要把上一节写好的二进制文件,放到本程序的同级目录下

cpp 复制代码
//二进制读文件
#include <iostream>
#include <fstream>  //1.包含头文件
using namespace std;

class Person{
public:
    char m_Name[64];
    int m_Age;
};

int main(){
    //2.创建流对象
    ifstream ifs;
    //3.打开文件并判断
    ifs.open("person.txt",ios::in | ios::binary);
    if( !ifs.is_open()){
        cout << "文件打开失败" << endl;
        return 0;
    }
    //4.读文件
    Person p;
    ifs.read( (char*)&p, sizeof(Person));
    cout << "姓名: " << p.m_Name << endl << "年龄: " << p.m_Age << endl;
    //5.关闭文件
    ifs.close();

    return 0;
}

3.总结

相关推荐
君鼎9 小时前
C++通用业务标准库中常用接口函数总结
c++·1024程序员节
杨筱毅9 小时前
【穿越Effective C++】条款5:了解C++默默编写并调用哪些函数——编译器自动生成的秘密
c++·effective c++·1024程序员节
落羽的落羽10 小时前
【Linux系统】从零掌握make与Makefile:高效自动化构建项目的工具
linux·服务器·开发语言·c++·人工智能·机器学习·1024程序员节
-森屿安年-10 小时前
STL 容器:List
开发语言·c++·list·1024程序员节
uxiang_blog10 小时前
C++进阶:继承
开发语言·c++
赵杰伦cpp10 小时前
数据结构——二叉搜索树深度解析
开发语言·数据结构·c++·算法
第七序章11 小时前
【C + +】unordered_set 和 unordered_map 的用法、区别、性能全解析
数据结构·c++·人工智能·算法·哈希算法·1024程序员节
草莓熊Lotso11 小时前
《算法闯关指南:优选算法--二分查找》--23.寻找旋转排序数组中的最小值,24.点名
开发语言·c++·算法·1024程序员节
sulikey12 小时前
Qt 入门简洁笔记:信号与槽
前端·c++·笔记·qt·前端框架·1024程序员节·qt框架
沐怡旸12 小时前
【穿越Effective C++】条款5:了解C++默默编写并调用哪些函数——编译器自动生成的秘密
c++