Posted on 2012-03-12 16:53
hoshelly 阅读(174)
评论(0) 编辑 收藏 引用 所属分类:
C
创建一个后缀名为txt的文件,并向该文件写入一个字符串,保存起来,再打开文件,读出文件中的内容。
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char pathName[20],txt1[100]={'\0'},txt2[20]={'\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 teh file: %s is\n",pathName);
fp=fopen(pathName,"r");
fread(txt2,fileLen,1,fp);
printf("%s\n",txt2);
return 0;
}