zoukankan      html  css  js  c++  java
  • C语言:用字符读取流和输出流来读写入数据。(文本文件)

    1. /*
    2.  文件的几种操作模式: r:只读   w:只写   rw:可读可写
    3.  文件的分类: t:文本文件(字符文件)   b:二进制文件(字节文件)
    4. 注意:
    5.  采用只读方式打开文件时,如果源文件不存在,打开文件会失败!
    6.  采用只写方式打开文件时,不管源文件存不存在,都不会失败。(因为会自动创建一个文件)
    7.  采用可读可写方式打开文件时,都会成功。
    8. */
    9. #include<stdio.h>
    10. int main()
    11. {
    12.       //定义文件指针
    13.       FILE *fpin = NULL;
    14.       FILE *fpout = NULL;
    15.       //打开文件
    16.      fpin = fopen("1.txt","wt");
    17.      if(fpin==NULL)
    18.      {
    19.          printf("文件打开失败! ");
    20.          return -1;
    21.      }
    22.      //文件写入
    23.      char cs;
    24.      while((cs = getchar())!='Q')
    25.      {
    26.               fputc(cs,fpin);
    27.      }
    28.      //文件关闭
    29.      fclose(fpin);
    30.     //打开文件
    31.     fpout = fopen("1.txt","rt");
    32.     if(fpout==NULL)
    33.     {
    34.               printf("文件打开失败! ");
    35.               return -1;
    36.     }
    37.     //文件读取
    38.     while(!feof(fpout))
    39.     {
    40.               //字符方式读文件
    41.               char cc = fgetc(fpout);
    42.               //用独处的字符和EOF进行比较来决定是否读到文件尾部
    43.                if(cc==EOF)
    44.                {
    45.                         break;
    46.                }
    47.                //输出字符
    48.                putchar(cc);
    49.     }
    50.     //文件关闭
    51.      fclose(fpout);
    52.      return 0;
    53. }
    54.  
  • 相关阅读:
    sentinel-initFunc&控制台
    Sentinel-FlowSlot
    Sentinel-AuthoritySlot&SystemSlot&LogSlot
    Sentinel-DegradeSlot
    Sentinel-ClusterBuilderSlot
    Sentinel-NodeSelectorSlot
    Sentinel整体架构
    Recyclers对象池设计
    加密算法的使用场景
    FastDFS分布式
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4708845.html
Copyright © 2011-2022 走看看