C 语言文件操作 / C++ 文件操作 / Linux 系统调用文件操作 全部带完整代码、注释、运行说明。

张开发
2026/4/11 5:45:28 15 分钟阅读

分享文章

C 语言文件操作 / C++ 文件操作 / Linux 系统调用文件操作 全部带完整代码、注释、运行说明。
一、C 语言标准文件操作stdio.h基于ANSI C 标准库跨平台Windows/Linux/macOS 通用。核心函数fopen() 打开文件fclose() 关闭文件fread() 读fwrite() 写fgets() / fputs() 行读写fprintf() / fscanf() 格式化读写ftell() / fseek() 偏移1. 写入文件#includestdio.hintmain(){// 打开文件w 写若不存在则创建存在则清空FILE*fpfopen(test_c.txt,w);if(fpNULL){perror(fopen failed);return1;}// 写入字符串fputs(Hello C file IO\n,fp);// 格式化写入fprintf(fp,int: %d, float: %.2f\n,100,3.14);// 二进制写入intbuf[]{1,2,3,4};fwrite(buf,sizeof(int),4,fp);fclose(fp);return0;}2. 读取文件#includestdio.hintmain(){FILE*fpfopen(test_c.txt,r);if(!fp){perror(fopen);return1;}charline[100];// 按行读while(fgets(line,sizeof(line),fp)){printf(%s,line);}// 移动到文件开头fseek(fp,0,SEEK_SET);// 格式化读inta;floatb;fscanf(fp,Hello C file IO\nint: %d, float: %f,a,b);printf(a%d, b%.2f\n,a,b);fclose(fp);return0;}3. 二进制读写示例#includestdio.htypedefstruct{intid;charname[20];}Student;intmain(){Student s{1,ZhangSan};FILE*fpfopen(student.bin,wb);fwrite(s,sizeof(Student),1,fp);fclose(fp);Student r;fpfopen(student.bin,rb);fread(r,sizeof(Student),1,fp);printf(id%d, name%s\n,r.id,r.name);fclose(fp);return0;}二、C 文件操作fstreamC 面向对象更安全、更易用。头文件#includefstream#includeiostream#includestring1. 文本写入#includefstream#includeiostreamusingnamespacestd;intmain(){ofstreamofs(test_cpp.txt);if(!ofs){cerropen failedendl;return1;}ofsHello C fstreamendl;ofs100 3.14endl;ofs.close();return0;}2. 文本读取#includefstream#includeiostream#includestringusingnamespacestd;intmain(){ifstreamifs(test_cpp.txt);string line;while(getline(ifs,line)){coutlineendl;}// 回到开头ifs.clear();ifs.seekg(0);inta;doubleb;ifslinelineab;couta bendl;ifs.close();return0;}3. 二进制读写#includefstreamusingnamespacestd;structStudent{intid;charname[20];};intmain(){Student s{2,LiSi};ofstreamofs(stu_cpp.bin,ios::binary);ofs.write((char*)s,sizeof(s));ofs.close();Student r;ifstreamifs(stu_cpp.bin,ios::binary);ifs.read((char*)r,sizeof(r));coutr.id r.nameendl;return0;}C 常用打开模式ios::out 写ios::in 读ios::app 追加ios::binary 二进制ios::trunc 清空ios::ate 打开后定位到末尾三、Linux 系统级文件操作系统调用基于 Linux 内核 API非标准 C只能在 Linux 用。头文件#includefcntl.h#includeunistd.h#includestdio.h#includeerrno.h核心函数open()close()read()write()lseek()stat()1. 写入文件#includefcntl.h#includeunistd.h#includestdio.hintmain(){// O_WRONLY 只写 | O_CREAT 创建 | 0644 权限intfdopen(linux_test.txt,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd-1){perror(open);return1;}constchar*strHello Linux syscall\n;write(fd,str,strlen(str));close(fd);return0;}2. 读取文件#includefcntl.h#includeunistd.h#includestdio.hintmain(){intfdopen(linux_test.txt,O_RDONLY);if(fd-1){perror(open);return1;}charbuf[1024];ssize_tnread(fd,buf,sizeof(buf)-1);if(n0){buf[n]0;printf(%s,buf);}close(fd);return0;}3. lseek 偏移// 跳到文件开头lseek(fd,0,SEEK_SET);// 跳到文件末尾lseek(fd,0,SEEK_END);// 从当前位置偏移lseek(fd,10,SEEK_CUR);4. 获取文件大小常用技巧off_tsizelseek(fd,0,SEEK_END);lseek(fd,0,SEEK_SET);printf(size %ld\n,size);四、三者核心区别超清晰总结特性C stdioC fstreamLinux 系统调用依赖stdio.hfstreamfcntl.h,unistd.h(Linux 专用)跨平台是是仅 Linux缓冲带用户态缓冲带缓冲无缓冲 (直接内核)风格函数式面向对象流系统调用文件描述符 fd速度较快较快灵活、底层、可极致优化安全性需手动检查 NULL异常/状态位必须检查 -1典型用途通用、跨平台C 项目、易用驱动、高性能服务器、系统编程五、编译运行命令Linux#Cgcc file.c -o file./file#Cg file.cpp -o file./file#Linux系统调用代码同样用gcc编译gcc linux.c -o linux./linux

更多文章