查看网站是否备案源码网站制作教程
查看网站是否备案,源码网站制作教程,网站建设需要学什么能力,qq在线登录聊天【C与Linux基础】文件篇 - 语言特性上的文件操作
在 C 中进行文件操作#xff0c;主要依赖两种方式#xff1a;
C 标准库#xff08;fstream#xff09;—— 现代 C 推荐方式#xff0c;跨平台#xff0c;面向对象风格C 风格文件操作#xff08;cstdioif(!ofs.is_open()){std::cerr无法打开文件\n;return1;}ofsHello, C file!\n;ofs当前行: __LINE__\n;ofs3.14159 42\n;// 也可以用格式化C20 之后更方便// ofs std::format(pi {:.4f}\n, 3.1415926535);ofs.close();// 通常可以省略析构时自动关闭// ------------------------------// 2. 读文件 - 按行读取最常用// ------------------------------std::ifstreamifs(output.txt);if(!ifs.is_open()){std::cerr无法打开文件\n;return1;}std::string line;while(std::getline(ifs,line)){std::coutline\n;}// ------------------------------// 3. 一次性读入全部内容适合小文件// ------------------------------std::ifstreamin(config.ini,std::ios::in|std::ios::binary);if(in){std::stringcontent((std::istreambuf_iteratorchar(in)),std::istreambuf_iteratorchar());std::cout文件全部内容长度: content.size()\n;}// ------------------------------// 4. 读写二进制文件// ------------------------------structRecord{intid;doublevalue;charname[32];};// 写std::ofstreambin_out(data.bin,std::ios::binary);Record r{1001,3.14159,SensorA};bin_out.write(reinterpret_castconstchar*(r),sizeof(r));// 读std::ifstreambin_in(data.bin,std::ios::binary);Record r2;if(bin_in.read(reinterpret_castchar*(r2),sizeof(r2))){std::coutr2.id r2.value r2.name\n;}return0;}C 文件流常用状态与标志打开模式说明常用组合std::ios::in以读方式打开读文件std::ios::out以写方式打开默认清空写文件std::ios::app追加模式日志文件std::ios::trunc如果文件存在则清空out 默认重写文件std::ios::binary二进制模式不转换换行符读写图片、struct、protobuf 等std::ios::ate打开后立即定位到文件末尾追加写2. C 风格文件操作仍然非常常见#includecstdio#includecstringintmain(){// 写文件FILE*fpfopen(log.txt,w);if(!fp){perror(fopen);return1;}fprintf(fp,Time: %ld Error: %s\n,time(nullptr),disk full);fputs(critical alert\n,fp);fclose(fp);// 读文件按块读charbuf[4096];FILE*infopen(large.bin,rb);if(in){size_t n;while((nfread(buf,1,sizeof(buf),in))0){// 处理 buf[0..n-1]}fclose(in);}// 定位 追加写FILE*logfopen(server.log,a);fseek(log,0,SEEK_END);// 可省略因为 a 模式默认追加fprintf(log,[INFO] connection from %s\n,192.168.1.100);return0;}3. Linux 系统调用方式底层、最灵活#includeunistd.h#includefcntl.h#includesys/stat.h#includesys/types.h#includecstring#includeiostreamintmain(){// 打开文件intfdopen(raw.dat,O_RDWR|O_CREAT,0644);if(fd-1){perror(open);return1;}// 写constchar*msgHello from syscall\n;write(fd,msg,strlen(msg));// 定位off_t poslseek(fd,0,SEEK_SET);if(pos-1)perror(lseek);// 读charbuf[128];ssize_t nread(fd,buf,sizeof(buf)-1);if(n0){buf[n]\0;std::coutRead: buf;}close(fd);// 追加写示例intlogfdopen(/var/log/app.log,O_WRONLY|O_APPEND|O_CREAT,0644);if(logfd!-1){constchar*lineERROR: connection timeout\n;write(logfd,line,strlen(line));close(logfd);}return0;}4. 三种方式对比总结2025–2026 视角方式跨平台易用性性能推荐场景现代 C 推荐度std::fstream是★★★★★中等日常开发、配置、日志、中等大小文件★★★★★C 风格FILE*是★★★★高与 C 库交互、性能敏感、嵌入式、老代码★★★Linux 系统调用否★★最高高性能服务器、驱动开发、大文件处理、零拷贝★★特定场景5. 现代 C 写文件操作的推荐实践2024优先使用std::ofstream/std::ifstream小文件直接读入std::string如上文方式大文件建议分块读取避免一次性读入全部内容总是检查打开是否成功RAII 原则尽量依赖析构自动关闭不显式close()日志类场景→ 考虑std::ofstreamstd::ios::app二进制数据→ 务必加上std::ios::binary异常安全→ 可开启ifs.exceptions(std::ios::failbit | std::ios::badbit);6. 常见问题快速定位文件打不开→ 检查路径、权限、是否被其他进程占用Windows 和 Linux 换行符不同→ 文本模式下自动处理二进制模式需注意写不进去→ 忘记flush()或close()或磁盘满读到乱码→ 忘记std::ios::binary或编码问题下一节可以深入讲RAII 封装的文件类mmap 内存映射文件异步文件 IOio_uring大文件分块读写 进度显示你更想往哪个方向继续或者有具体场景日志、配置、二进制、超大文件等想看最优实现