(C++) 文件读写基础

🗂️前言

📄ref

📄访问标记

🗃️流打开模式类型

std::ios_base::openmode - cppreference.com

常量 描述
app 每次写入前寻位到流结尾
binary 二进制模式打开
in 为读打开
out 为写打开
trunc 在打开时舍弃流的内容
ate 打开后立即寻位到流结尾
noreplace (C++23) 以独占模式打开

🗂️Code

📄demo

#include <cstring>
#include <fstream>
#include <iostream>

void check_io(std::ios& io) {
    if (io.good()) {
        std::cerr << " io is good." << std::endl;
    }
    if (io.fail()) {
        std::cerr << " io is fail." << std::endl;
    }
    if (io.bad()) {
        std::cerr << " io is bad." << std::endl;
    }
    if (io.eof()) {
        std::cerr << " io is eof." << std::endl;
    }
    io.clear();
}

void file_write(std::string file_path) {
    std::ofstream ofs;
    ofs.open(file_path, std::ios::out);
    if (!ofs.is_open()) {
        std::cerr << "File Open Error" << std::endl;
        return;
    }

    char msg[] = "https://space.bilibili.com/8172252";
    ofs << "cuber-lotus" << std::endl;
    for (char ch : {'u', 'r', 'l', ':'}) {
        ofs.put(ch);
    }
    ofs.write(msg, strlen(msg));

    std::cout << __func__;
    check_io(ofs);
    ofs.close();
}

void file_read(std::string file_path) {
    std::ifstream ifs;
    ifs.open(file_path, std::ios::in);
    if (!ifs.is_open()) {
        std::cerr << "File Open Error" << std::endl;
        return;
    }

#define CASE 1

#if CASE == 1
    char buf[1024] = {};
    while (ifs >> buf) {
        std::cout << buf << std::endl;
    }
#elif CASE == 2
    char buf[1024] = {};
    while (ifs.getline(buf, sizeof(buf))) {
        std::cout << buf << std::endl;
    }
#elif CASE == 3
    std::string buf;
    while (std::getline(ifs, buf)) {
        std::cout << buf << std::endl;
    }
#elif CASE == 4
    int ch;
    while ((ch = ifs.get()) != EOF) {
        std::cout << (char)ch;
    }
    std::cout << std::endl;
#elif CASE == 5
    char buf[1024] = {};
    while (ifs.read(buf, sizeof(buf))) {
        std::cout << buf << std::endl;
    }
#endif

    std::cout << __func__;
    check_io(ifs);
    ifs.close();
}

int main() {
    const std::string file_path = "example.txt";
    file_write(file_path);
    file_read(file_path);
}

📄分点讲解

C++用类封装了流式操作,使用体验是更加丰富,更加多。
在这里插入图片描述

🗃️打开/关闭

// fstream 可以同时管理 i/o
std::fstream fs;
fs.open(file_path, std::ios::out | std::ios::in);
if (!fs.is_open()) {
    std::cerr << "File Open Error" << std::endl;
    return;
}
fs.close();

🗃️写

法1:

ofs << "cuber-lotus" << std::endl;

法2:

char msg[] = "https://space.bilibili.com/8172252";
ofs.write(msg, strlen(msg));

法3:

ofs.put(ch);

🗃️读

法1:

char buf[1024] = {};
while (ifs >> buf) {
    std::cout << buf << std::endl;
}

法2:

char buf[1024] = {};
while (ifs.getline(buf, sizeof(buf))) {
    std::cout << buf << std::endl;
}

法3:

std::string buf;
while (std::getline(ifs, buf)) {
    std::cout << buf << std::endl;
}

法4:

int ch;
while ((ch = ifs.get()) != EOF) {
    std::cout << (char)ch;
}
std::cout << std::endl;

法5:

char buf[1024] = {};
while (ifs.read(buf, sizeof(buf))) {
    std::cout << buf << std::endl;
}

🗃️状态函数

状态函数
good 检查是否没有发生错误,即是否可执行输入/输出操作 (std::basic_ios<CharT,Traits> 的公开成员函数)
eof 检查是否到达了文件末尾 (std::basic_ios<CharT,Traits> 的公开成员函数)
fail 检查是否发生了可恢复的错误 (std::basic_ios<CharT,Traits> 的公开成员函数)
bad 检查是否已发生不可恢复的错误 (std::basic_ios<CharT,Traits> 的公开成员函数)
operator! 检查是否有错误发生(fail() 的同义词) (std::basic_ios<CharT,Traits> 的公开成员函数)
operator bool 检查是否没有发生错误(!fail() 的同义词) (std::basic_ios<CharT,Traits> 的公开成员函数)
rdstate 返回状态标志 (std::basic_ios<CharT,Traits> 的公开成员函数)
setstate 设置状态标志 (std::basic_ios<CharT,Traits> 的公开成员函数)
clear 修改状态标志 (std::basic_ios<CharT,Traits> 的公开成员函数)



🗂️END

🌟关注我

关注我,学习更多C/C++,算法,计算机知识

B站:

👨‍💻主页:天赐细莲 bilibili

相关推荐

  1. c++的文件

    2024-07-23 08:44:01       53 阅读
  2. C++的文件

    2024-07-23 08:44:01       65 阅读
  3. C++BMP文件

    2024-07-23 08:44:01       29 阅读
  4. C语言】文件

    2024-07-23 08:44:01       42 阅读
  5. C++二进制文件

    2024-07-23 08:44:01       29 阅读
  6. C# —— File文件

    2024-07-23 08:44:01       22 阅读
  7. 1.基于C#的Dbf文件结构概述)

    2024-07-23 08:44:01       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 08:44:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 08:44:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 08:44:01       45 阅读
  4. Python语言-面向对象

    2024-07-23 08:44:01       55 阅读

热门阅读

  1. Torus结构代码实现

    2024-07-23 08:44:01       17 阅读
  2. linux命令-touch-修改文件时间

    2024-07-23 08:44:01       14 阅读
  3. Oracle(17)什么是物化视图(Materialized View)?

    2024-07-23 08:44:01       14 阅读
  4. Electron 和 React 开发桌面应用程序

    2024-07-23 08:44:01       16 阅读
  5. (20240721)无机非金属材料工学(3)混凝土

    2024-07-23 08:44:01       15 阅读
  6. golang长连接的误用

    2024-07-23 08:44:01       15 阅读
  7. ubuntu开启 远程登录 允许root远程登录

    2024-07-23 08:44:01       15 阅读
  8. P1725 琪露诺 题解

    2024-07-23 08:44:01       18 阅读
  9. Qt 实战(7)元对象系统 | 7.6、Q_DECLARE_METATYPE详解

    2024-07-23 08:44:01       15 阅读
  10. php 根据位置的经纬度计算距离

    2024-07-23 08:44:01       14 阅读