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

源码

   1: #include <stdio.h>
   2: #include <string.h>
   3:  
   4: int main()
   5: {
   6:     FILE *fp;
   7:     char pathname[20], txt1[20]={'\0'}, txt2[20]={'\0'};
   8:     int nFileLen;
   9:  
  10:     printf("please input the path name of the file\n");
  11:     scanf("%s", pathname);
  12:     fp = fopen(pathname, "w");
  13:  
  14:     printf("please input a string to this file\n");
  15:     scanf("%s", txt1);
  16:     nFileLen = strlen(txt1);
  17:     fwrite(txt1, nFileLen, 1, fp);
  18:     fclose(fp);
  19:     printf("The file has been saved\n");
  20:  
  21:     printf("The content of the file: %s is \n", pathname);
  22:     fp = fopen(pathname, "r");
  23:     fread(txt2, nFileLen, 1, fp);
  24:     printf("%s\n", txt2);
  25:     fclose(fp);
  26:  
  27:     return 0;
  28: }
posted @ 2010-05-24 09:47  红脸书生  阅读(423)  评论(0编辑  收藏  举报