c语言关于文件的读写操作

it2024-07-04  43

读操作

读操作主要用的是fscanf函数,fscanf函数有个特点,就是碰到空格和换行会自动中断当前变量的读取。然后下次读操作自动跳过空格和换行。

代码 #include <stdio.h> #include <stdlib.h> int main() { FILE *fw = fopen("D:\\data.txt", "r"); int val; while (fscanf(fw, "%d", &val) == 1) { printf("%d\n", val); } fcolse(fw); system("pause"); return 0; } 样例图

写操作

写操作主要调用的是fprintf函数

代码 #include <stdio.h> #include <stdlib.h> int main() { FILE *fw = fopen("D:\\data.txt", "w"); fprintf(fw, "%d\t", 1234); fprintf(fw, "%d\n%d",1234,1234); fclose(fw); system("pause"); return 0; } 样例图

最新回复(0)