IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++文件操作 -> 正文阅读

[C++知识库]c++文件操作

文件操作

文件分类

  1. 文本文件
  2. 二进制文件

文件的打开方式

操作文件的三大类

  1. ofstream:写操作
  2. ifstream:读操作
  3. fstream:读写操作

文本文件

写文本文件

#include<iostream>

using namespace std;
//1、包含头文件
#include <fstream>

/**
 * 写文件
 */
void test01() {
    //2、创建流对象
    ofstream ofstream1;

    //3、指定打开方式,out表示输出
    ofstream1.open("test.txt",ios::out);

    //4、写内容    
    ofstream1 << "姓名:张三" << endl;
    ofstream1 << "年龄:18" << endl;
    ofstream1 << "工作:玩游戏" << endl;

    //5、关闭文件
    ofstream1.close();
}

int main(){
    test01();
}

读文本文件

#include<iostream>

using namespace std;
//1、包含头文件
#include <fstream>
#include "string"

void test() {
    //2、创建流对象
    ifstream ifstream1;

    //3、打开文件
    ifstream1.open("test.txt", ios::in);

    //判断文件是否打开成功
    if (!ifstream1.is_open()) {
        cout << "the file opening failure!";
    } else {
        //4、读数据方式一
        /*     char buf[1024] = {0};
               while (ifstream1 >> buf) {
                   cout << buf << endl;
               }*/

        //方式二
        /*char buf[1024] = {0};
        while (ifstream1.getline(buf, sizeof(buf))) {
            cout << buf << endl;
        }*/

        //方式三
        /* string buf;
         while (getline(ifstream1, buf)) {
             cout << buf << endl;
         }*/

        //方法4,EOF:end of file(文件尾)
        char c;
        while ((c = ifstream1.get()) != EOF) {
            cout << c;
        }
    }

    //关闭流
    ifstream1.close();
}

int main() {
    test();
}

二进制文件

写二进制文件

#include<iostream>

using namespace std;
//1、引入头文件
#include<fstream>

class Person {
public:
    char name[64];
    int age;
};

void test01() {
    //2、创建流对象
    ofstream ofs;

    //3、打开文件,输出二进制文件
    ofs.open("person.txt", ios::out | ios::binary);

    Person person = {"Tom", 18};

    //4、写文件
    ofs.write((const char *) &person, sizeof(Person));

    //关闭文件
    ofs.close();
}

int main(){
     test01();
}

读二进制文件

#include<iostream>
//1、引入头文件
#include<fstream>

using namespace std;

class Person {
public:
    char name[64];
    int age;
};

void test() {
    //2、创建流对象
    ifstream ifs;
    //3、打开文件
    ifs.open("person.txt", ios::in | ios::binary);
    //4、判断文件是否打开
    if (ifs.is_open()) {
        Person p;
        ifs.read((char *) &p, sizeof(Person));
        cout << "name:" << p.name << "\nage:" << p.age << endl;
    } else {
        cout << "File opening failure!" << endl;
        return;
    }
    //5、关闭文件
    ifs.close();
}

int main() {
    test();
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:03:37  更:2021-08-09 10:04:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 13:14:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码