您的位置:首页 > 编程语言

编程基本功——C标准文件的读写操作示例

2010-05-24 09:47 323 查看

源码

#include <stdio.h>
#include <string.h>
int main()
{
 FILE *fp;
 char pathname[20], txt1[20]={'\0'}, txt2[20]={'\0'};
 int nFileLen;
 printf("please input 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);
 nFileLen = strlen(txt1);
 fwrite(txt1, nFileLen, 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, nFileLen, 1, fp);
 printf("%s\n", txt2);
 fclose(fp);
 return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: