创建一个后缀名为txt的文件,并向该文件中写入一个字符串,保存起来。再打开该文件,读出文件中内容。
其实这个是使用C语言提供的读写文件的API的应用,多多练习应该就会用了。代码如下:
- #include <stdio.h>
-
#include <string.h>
-
-
int main(int argc, char *argv[])
-
{
-
FILE *fp;
-
char pathName[50], txt1[100] = {'\0'}, txt2[100] = {'\0'};
-
int fileLen;
-
printf("please type the path name of the file\n");
-
scanf("%s",pathName);
-
fp = fopen(pathName,"w");
-
printf("please input a string to this file\n");
-
scanf("%s",txt1);
-
fileLen = strlen(txt1);
-
fwrite(txt1, fileLen, 1, fp);
-
fclose(fp);
-
printf("the file has been saved\n");
-
printf("the content of the file: %s is \n",pathName);
-
fp = fopen(pathName, "r");
-
fread(txt2, fileLen, 1,fp);
-
printf("%s\n",txt2);
-
return 0;
-
}
阅读(881) | 评论(0) | 转发(0) |