开发者

一文详解C++中打开文件的多种方式及相关流类

目录
  • 前言
  • 1. 使用标准文件流类(<fstream>)
    • 1.1ofstream- 输出文件流(写入)
    • 1.2ifstream- 输入文件流(读取)
    • 1.3fstream- 双向文件流(读写)
  • 2. 使用 C 风www.devze.com格文件操作(<cstdio>)
    • 3. 使用文件描述符(POSIX/unistd.h)
      • 4. 使用内存映射文件(高级方式)
        • 5. 文件打开模式详解
          • 5.1 C++ 流打开模式(位掩码组合)
          • 5.2 C 风格打开模式
        • 6. 不同方式的比较
          • 7. 最佳实践建议
            • 8. 高级用法示例
              • 8.1 同时读写文件
              • 8.2 二进制文件操作

            前言

            打开一个文件的方式都有哪些,都可以通过哪些流来获取它?

            在 C++ 中,打开文件可以通过多种流类实现,每种方式都有其特定的用途和特点。以下是详细的分类和www.devze.com说明:

            1. 使用标准文件流类(<fstream>)

            1.1ofstream- 输出文件流(写入)

            #include <fstream>
            
            // 方式1:构造函数直接打开
            std::ofstream outFile1("output.txt"); // 默认模式:IOS::out | ios::trunc
            
            // 方式2:先创建后打开
            std::ofstreaphpm outFile2;
            outFile2.open("output.txt", std::ios::app); // 追加模式
            
            // 写入数据
            outFile1 << "Hello Worldjs" << std::endl;

            1.2ifstream- 输入文件流(读取)

            #include <fstream>
            
            // 方式1:构造函数直接打开
            std::ifstream inFile1("input.txt"); // 默认模式:ios::in
            
            // 方式2:先创建后打开
            std::ifstream inFile2;
            inFile2.open("input.txt");
            
            // 读取数据
            std::string line;
            while (std::getline(inFile2, line)) {
                std::cout << line << std::endl;
            }

            1.3fstream- 双向文件流(读写)

            #include <fstream>
            
            // 读编程客栈写模式
            std::fstream ioFile("data.txt", std::ios::in | std::ios::out);
            
            // 读写二进制文件
            std::fstream binFile("data.bin", 
                                std::ios::binary | 
                                std::ios::in | 
                                std::ios::out);

            2. 使用 C 风格文件操作(<cstdio>)

            #include <cstdio>
            
            // 打开方式
            FILE* file = fopen("file.txt", "r"); // 读取
            FILE* file = fopen("file.txt", "w"); // 写入(清空)
            FILE* file = fopen("file.txt", "a"); // 追加
            FILE* file = fopen("file.bin", "rb"); // 二进制读取
            
            if (file != nullptr) {
                // 使用文件...
                fclose(file); // 必须手动关闭
            }

            3. 使用文件描述符(POSIX/unistd.h)

            #include <unistd.h>
            #include <fcntl.h>
            
            int fd = open("file.txt", O_RDONLY); // 只读
            int fd = open("file.txt", O_WRONLY | O_CREAT, 0644); // 写入+创建
            
            if (fd != -1) {
                char buffer[1024];
                read(fd, buffer, sizeof(buffer));
                close(fd); // 必须手动关闭
            }

            4. 使用内存映射文件(高级方式)

            #include <sys/mman.h>
            #include <fcntl.h>
            #include <unistd.h>
            
            int fd = open("largefile.bin", O_RDONLY);
            void* mapped = mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
            
            // 使用映射的内存区域...
            munmap(mapped, file_size);
            close(fd);

            5. 文件打开模式详解

            5.1 C++ 流打开模式(位掩码组合)

            模式标志描述
            std::ios::in读取模式
            std::ios::out写入模式
            std::ios::app追加写入
            std::ios::ate打开后定位到文件末尾
            std::ios::trunc如果文件存在则清空
            std::ios::binary二进制模式

            5.2 C 风格打开模式

            模式描述
            "r"只读
            "w"只写(创建/清空)
            "a"追加写入
            "r+"读写
            "w+"读写(创建/清空)
            "a+"读和追加

            6. 不同方式的比较

            特性C++ 流C 文件操作文件描述符
            面向对象
            类型安全
            自动资源管理
            格式化I/O
            二进制I/O
            低级控制部分
            跨平台基本✓
            性能中等较高最高

            7. 最佳实践建议

            常规文本文件操作:优先使用 C++ 文件流(fstream/ofstream/ifstream

            高性能需求:考虑使用 C 风格文件操作或文件描述符

            二进制文件:总是使用 std::ios::binary 标志

            资源管理:使用 RAII 原则,推荐 C++ 流自动管理资源

            错误检查:始终检查文件是否成功打开

            std::ofstream file("data.txt");
            if (!file.is_open()) {
                // 错误处理
            }

            8. 高级用法示例

            8.1 同时读写文件

            std::fstream file("data.txt", 
                             std::ios::in | 
                             std::ios::out |
                             std::ios::ate); // 打开并定位到末尾
            
            if (file) {
                // 读取当前位置
                std::streampos endPos = file.tellg();
                
                // 回到开头读取
                file.seekg(0);
                std::string content;
                std::getline(file, content);
                
                // 写入新数据
                file << "\nNew data appended";
            }

            8.2 二进制文件操作

            struct Record {
                int id;
                char name[50];
                double value;
            };
            
            // 写入二进制
            std::ofstream binOut("data.bin", std::ios::binary);
            Record rec{1, "Test", 3.14};
            binOut.write(reinterpret_cast<char*>(&rec), sizeof(Record));
            
            // 读取二进制
            std::ifstream binIn("data.bin", std::ios::binary);
            Record inRec;
            binIn.read(reinterpret_cast<char*>(&inRec), sizeof(Record));

            选择哪种方式取决于具体需求,C++ 文件流提供了最安全和方便的方式,而 C 风格和文件描述符则提供了更多的控制和更高的性能。

            到此这篇关于一文详解C++中打开文件的多种方式及相关流类的文章就介绍到这了,更多相关C++打开文件内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

            0

            上一篇:

            下一篇:

            精彩评论

            暂无评论...
            验证码 换一张
            取 消

            最新开发

            开发排行榜