C++文件操作

简介: C++文件操作

文本文件:文件以文本的ASCII码形式存储在计算机中;

二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂他们。

一、文本文件

1、写文件

#include <iostream>
#include <string>
#include <fstream> //包含文件头
 
using namespace std;
 
//文本文件,写文件
void test01() {
    //创建流对象
    ofstream ofs;
    //打开文件 为写文件而打开文件
    ofs.open("test.txt", ios::out);
    //写数据
    ofs << "aaaa" << endl;
    ofs << "bbbb" << endl;
    ofs << "cccc" << endl;
    ofs << "dddd" << endl;
    //关闭文件
    ofs.close();
}
 
int main() {
    test01();
    return 0;
}
 
 

2、读文件

#include <iostream>
#include <string>
#include <fstream> //包含文件头
 
using namespace std;
 
//文本文件,读文件 推荐使用前三种方法
void test01() {
    //创建流对象
    ifstream ifs;
    //打开文件并判断文件是否打开成功
    ifs.open("test.txt", ios::in);
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return;
    }
    //读数据
    //第一种
//    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;
    }
    //关闭文件
    ifs.close();
}
 
int main() {
    test01();
    return 0;
}
 
 
aaaa
bbbb
cccc
dddd

二、二进制文件

1、写文件

#include <iostream>
#include <fstream> //包含文件头
 
using namespace std;
 
//二进制文件 写文件 避免使用string
class Person {
public:
    char m_Name[64];//姓名
    int m_Age;//
};
 
void test01() {
//创建流对象、打开文件
    ofstream ofs("person.txt", ios::out | ios::binary);
//写文件
    Person p = {"zhangsan", 18};
    ofs.write((const char *) &p, sizeof(p));
//关闭文件
    ofs.close();
}
 
int main() {
    test01();
    return 0;
}
 
 

2、读文件

#include <iostream>
#include <fstream> //包含文件头
 
using namespace std;
 
//二进制文件 读文件 避免使用string
class Person {
public:
    char m_Name[64];//姓名
    int m_Age;//
};
 
void test01() {
//创建流对象、打开文件
    ifstream ifs("person.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return;
    }
//写文件
    Person p;
    ifs.read((char *) &p, sizeof(Person));
    cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
//关闭文件
    ifs.close();
}
 
int main() {
    test01();
    return 0;
}
 
 
姓名:zhangsan 年龄:18
目录
相关文章
|
1月前
|
安全 算法 程序员
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
81 0
|
1月前
|
存储 C++ iOS开发
C++文件操作
C++文件操作
|
1月前
|
移动开发 Linux 程序员
c++文件操作,超详细
c++文件操作,超详细
60 0
|
28天前
|
C++ iOS开发
C++ 文件操作的技术性文章
C++ 文件操作的技术性文章
15 0
|
1月前
|
存储 C语言 数据安全/隐私保护
C++中的文件操作技术详解
C++中的文件操作技术详解
|
1月前
|
C++
C++语言学习文件操作应用案例
C++文件操作示例:创建`ofstream`对象写入&quot;Hello, World!&quot;到`output.txt`,刷新缓冲区,然后使用`ifstream`读取并打印文件内容。如果文件打开失败,程序将显示错误信息并返回1。
20 3
|
1月前
|
C++
深入理解 C++ 中的多态与文件操作
C++中的多态是OOP核心概念,通过继承和虚函数实现。虚函数允许对象在相同操作下表现不同行为,提高代码可重用性、灵活性和可维护性。例如,基类`Animal`声明`makeSound()`虚函数,派生类如`Cat`、`Dog`和`Bird`可重写该函数实现各自叫声。C++也提供多种文件操作,如`fstream`库的`ofstream`、`ifstream`用于读写文件,C++17引入的`&lt;filesystem&gt;`库提供更现代的文件操作接口。
32 0
|
1月前
|
存储 编译器 程序员
【C++ 文件操作与字符串处理】从文件读取到内容分割的全方位指南
【C++ 文件操作与字符串处理】从文件读取到内容分割的全方位指南
206 6
|
1月前
|
存储 C++
C++从入门到精通:2.3.1文件操作
C++从入门到精通:2.3.1文件操作
|
1月前
|
存储 C++ iOS开发
C++文件操作(文本文件的读写+二进制文件的读写)
C++文件操作(文本文件的读写+二进制文件的读写)