Posted on 2012-03-12 16:53
hoshelly 閱讀(181)
評論(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;
}