C++文件输入输出编程

📅 2026/6/18 0:38:34
C++文件输入输出编程
C文件输入输出编程文件操作是程序与外部存储交互的主要方式。C通过fstream库提供了统一的流式文件读写接口支持文本和二进制模式。ifstream读取文件ofstream写入文件。#include#include#include#include#includevoid file_write() {std::ofstream out(data.txt);if (!out) {std::cerr Cannot open file for writing\n;return;}out Line 1: Hello C\n;out Line 2: File I/O operations\n;out Line 3: 42 \n;out Line 4: Pi 3.14159 \n;out.close();std::cout File written successfully\n;}void file_read() {std::ifstream in(data.txt);if (!in) {std::cerr Cannot open file for reading\n;return;}std::string line;int line_num 0;while (std::getline(in, line)) {std::cout Line line_num : line \n;}in.close();}void file_append() {std::ofstream out(log.txt, std::ios::app);if (out) {out Log entry at: __TIME__ \n;out.close();}}二进制文件读写。struct Record {int id;char name[32];double value;};void binary_write() {Record records[] {{1, Alpha, 100.5},{2, Beta, 200.7},{3, Gamma, 300.9}};std::ofstream out(records.bin, std::ios::binary);if (out) {out.write(reinterpret_cast(records), sizeof(records));out.close();std::cout Binary file written\n;}}void binary_read() {Record records[3];std::ifstream in(records.bin, std::ios::binary);if (in) {in.read(reinterpret_cast(records), sizeof(records));in.close();for (const auto r : records) {std::cout ID: r.id , Name: r.name , Value: r.value \n;}}}fstream同时读写。void read_write_file() {std::fstream file(data.txt, std::ios::in | std::ios::out | std::ios::app);if (!file) {std::cerr Failed to open\n;return;}file Appended line\n;file.seekg(0, std::ios::beg);std::string line;while (std::getline(file, line)) {std::cout line \n;}file.close();}stringstream在内存中操作字符串。void stringstream_demo() {std::stringstream ss;ss Name: Alice\n;ss Age: 30 \n;ss Score: 95.5 \n;std::cout String stream:\n ss.str();std::string name;int age;double score;ss.clear();ss.seekg(0);std::string label;ss label name;ss label age;ss label score;std::cout Parsed: name , age , score \n;}文件流状态检查。void stream_status() {std::ifstream file(nonexistent.txt);if (file.fail()) {std::cout Fail bit set (file not found)\n;file.clear();}file.open(data.txt);if (file.good()) {std::cout File is good\n;int num;file num;if (file.fail()) {std::cout Failed to read integer\n;file.clear();}}}文件定位。void file_positioning() {std::ofstream out(positions.txt);out 0123456789ABCDEF;out.close();std::fstream file(positions.txt, std::ios::in | std::ios::out);if (file) {file.seekp(5, std::ios::beg);file HELLO;file.seekg(0, std::ios::end);std::streampos size file.tellg();std::cout File size: size bytes\n;file.seekg(2, std::ios::beg);char ch;file.get(ch);std::cout Char at position 2: ch \n;file.close();}}CSV文件处理。void csv_processing() {std::ofstream out(data.csv);out Name,Age,Score\n;out Alice,30,95.5\n;out Bob,25,87.0\n;out Charlie,35,92.3\n;out.close();std::ifstream in(data.csv);std::string line;while (std::getline(in, line)) {std::stringstream ss(line);std::string cell;while (std::getline(ss, cell, ,)) {std::cout cell \t;}std::cout \n;}}文件读取到vector。std::vector read_numbers(const std::string filename) {std::vector numbers;std::ifstream file(filename);int num;while (file num) {numbers.push_back(num);}return numbers;}文件操作是程序持久化的基础掌握流式I/O模型对数据处理至关重要。