一、打开文件
[c] #include <stdio.h> #include <stdlib.h> main(){ FILE *fp; fp = fopen("test", "r"); if(fp == NULL){ // 打开操作不成功 printf("The file can not be opened.\n"); exit(1); } } [/c]
二、读取文件
[c] #include <stdio.h> #include <stdlib.h> main(){ int str; FILE *fp; fp = fopen("test", "r"); str = fgetc(fp); // 读取一个字符 if(str != EOF){ putchar(str); } fclose(fp); } [/c]
三、读取一行
[c] #include <stdio.h> #include <stdlib.h> main(){ char str[1024]; FILE *fp; fp = fopen("test", "r"); fgets(str, 1024, fp); printf("%s\n", str); fclose(fp); } [/c]
四、写入文件
[c] #include <stdio.h> main(){ FILE *fp; fp = fopen("test", "w"); fputs("this is test\n", fp); fclose(fp); } [/c]
五、其他