读取写入文件三种方法
按照字符读取和写入
#include<stdio.h> #include<string.h> #include<stdlib.h> int fputc_func(char *filename) { int i = 0; FILE *fp = NULL; char buf[64] = "this is function that writting file by char"; fp = fopen(filename,"w+"); if (NULL == fp) { printf("func open file error\n"); return -1; } int len = strlen(buf); for (i = 0; i < len; i++) { fputc(buf[i],fp); } fclose(fp); return 0; } int fgetc_func(char *filename) { int i = 0; FILE *fp = NULL; char buf[64]; fp = fopen(filename, "r+"); if (NULL == fp) { printf("func fopen() error\n"); return -1; } while (!feof(fp)) { char temp = fgetc(fp); printf("%c",temp); } if (fp != NULL) { fclose(fp); } return 0; } int main() { char *filename = "char.txt"; fputc_func(filename); fgetc_func(filename); return 0; }
按照行读取写入
#include<stdio.h> #include<string.h> #include<stdlib.h> int fputs_func() { int i = 0; FILE *fp = NULL; char *filename = "./char.txt"; char buf[32] = "abcdefghijklmn"; fp = fopen(filename,"w+"); //open file with both read and write //if the file not exit, then create if (NULL == fp) { printf("func_puts fopen error \n"); return -1; } fputs(buf, fp); fclose(fp); return 0; } int fget_func() { int i = 0; FILE *fp = NULL; char *filename = "char.txt"; char buf[1024]; /* open file with both read and write, if the file not is exist, then return a error */ fp = fopen(filename, "r+"); if (NULL == fp) { printf("func_fget fopen() error"); return -1; } while (!feof(fp)) { char *p = fgets(buf,1024,fp); if (p == NULL) { break; } printf("%s",buf); } if (fp != NULL) { fclose(fp); } return 0; } int main() { fputs_func(); fget_func(); return 0; }
按照块读取写入
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct Teacher { char name[64]; int age; }Teacher; int fwrite_func(char *filename) { int i = 0; FILE *fp = NULL; Teacher tArray[3]; int myN = 0; for (i = 0 ; i < 3; i++) { sprintf(tArray[i].name, "%d%d%d", i+1, i+1, i+1); tArray[i].age = i + 31; } fp = fopen(filename, "wb"); //write file with binary if (fp == NULL) { printf("create file error\n"); return; } for (i = 0; i < 3; i++) { myN = fwrite(&tArray[i], sizeof(Teacher), 1, fp); } if (fp != NULL) { fclose(fp); } return 0; } int fread_func(char *filename) { int i = 0; FILE *fp = NULL; int myN = 0; Teacher tArray[3]; fp = fopen(filename, "r+b"); if (fp == NULL) { printf("create file error\n"); return -1; } for (i = 0; i < 3; i++) { myN = fread(&tArray[i], sizeof(Teacher), 1, fp); } for (i = 0; i < 3; i++) { printf("name:%s, age:%d\n",tArray[i].name,tArray[i].age); } if (fp != NULL) { fclose(fp); } return 0; } int main() { char *filename = "bolck.txt"; fwrite_func(filename); fread_func(filename); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)