#include#define MAIN_FILE_PATH "test.txt" //文本放在.c相同路径 #define WORDS_NUMS 100 //宏定义 int main() { FILE *fp; char str[WORDS_NUMS + 1];//多加1是指符号 fp = fopen(MAIN_FILE_PATH, "rt"); if (fp == NULL) { printf("ERROR:open a null file"); return 0; } while(fgets(str, WORDS_NUMS, fp) != NULL){ printf("%s", str); } fclose(fp); fp = fopen(MAIN_FILE_PATH, "w"); if (fp == NULL) { printf("ERROR:open a null file"); return 0; } char strWrite[] = "abcd"; char strRead[100]; fwrite(str, sizeof(char), sizeof(strWrite), fp); fread(strRead, sizeof(char), sizeof(strWrite), fp); fclose(fp); return 0; }
C语言文件
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)