标准文件I/O操作

FILE 对象包含信息
    文件描述符
    该流的缓冲区
    缓冲区长度
    当前缓冲区的字节数
    出错标志
    
#include <wchar.h> 改变流的定向
int fwide(FILE *stream, int mode);
    
mode:
    mode>0 宽字节流、
    mode<0 字节流
返回值:
    > 0 宽字节流、
    < 0 字节流

全缓冲:缓冲区满时执行I/O操作
行缓冲:遇见换行符时执行I/O操作
不带缓冲:立即执行I/O操作

更改缓冲类型函数:
    
    #include <stdio.h>

       void setbuf(FILE *stream, char *buf);

       void setbuffer(FILE *stream, char *buf, size_t size);

       void setlinebuf(FILE *stream);

       int setvbuf(FILE *stream, char *buf, int mode, size_t size);

mode:
    _IONBF unbuffered 不带缓冲

    _IOLBF line buffered 行缓冲

    _IOFBF fully buffered 全缓冲
    
size(buf) ==0   <=>不带缓冲

强制冲刷一个流:
    #include <stdio.h>
    int fflush(FILE *stream);

文件的打开:
       #include <stdio.h>
    从指定路径打开一个文件:
       FILE *fopen(const char *path, const char *mode);
    指定一个文件打开一个预定义的流:
       FILE *fdopen(int fd, const char *mode);
    从已有的文件描述符并使一个标准的I/O流该描述符相结合;通常用于创建管道和socket文件描述符:
       FILE *freopen(const char *path, const char *mode, FILE *stream);

mode:
       r      Open text file for reading.  The stream is positioned at the beginning of the file.

       r+     Open for reading and writing.  The stream is positioned at the beginning of the file.

       w      Truncate  file  to zero length or create text file for writing.  The stream is positioned at the begin‐
              ning of the file.

       w+     Open for reading and writing.  The file is created if it does not exist,  otherwise  it  is  truncated.
              The stream is positioned at the beginning of the file.

       a      Open  for appending (writing at end of file).  The file is created if it does not exist.  The stream is
              positioned at the end of the file.

       a+     Open for reading and appending (writing at end of file).  The file is created if  it  does  not  exist.
              The initial file position for reading is at the beginning of the file, but output is always appended to
              the end of the file.
              
文件的关闭:
       #include <stdio.h>

       int fclose(FILE *fp);
一次读取一个字符:
           #include <stdio.h>

       int fgetc(FILE *stream);

       int getc(FILE *stream);

       int getchar(void);
       可以将读取的数据据再次压入到流中:int ungetc(int c, FILE *stream);

           
       
单字符输出函数:
       #include <stdio.h>

       int fputc(int c, FILE *stream);

       int putc(int c, FILE *stream);

       int putchar(int c);
       
输入一行:
    int gets(char *buf); 从标准输入流读
    int fgets(char *buf,int len ,FILE * fp);
    
输出一行:
       int puts(const char *s); 把一个以null结尾的字符串输出到标准输出;
       int fputs(const char *s, FILE *stream); 把一个以null结尾的字符串输出到指定的流,null终止符不输出;

二进制I/O操作:
    
    #include <stdio.h>

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

       size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

流定位:
     #include <stdio.h>

       int fseek(FILE *stream, long offset, int whence);

       long ftell(FILE *stream);

       void rewind(FILE *stream); 定位到文件的开始位置:

       int fgetpos(FILE *stream, fpos_t *pos);
       int fsetpos(FILE *stream, const fpos_t *pos);
       
    whence:
        SEEK_SET
        SEEK_CUR
        SEEK_END    

格式话输出:
    
    #include <stdio.h>

       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
       int sprintf(char *str, const char *format, ...);
       int snprintf(char *str, size_t size, const char *format, ...);
格式化输入:
    #include <stdio.h>

       int scanf(const char *format, ...);
       int fscanf(FILE *stream, const char *format, ...);

       int sscanf(const char *str, const char *format, ...);



#include<stdio.h>
void stdfileTest()
{
   FILE *fd =  fopen("b.txt","a+");
   if(fd == NULL )
        cout<<"oepn fail"<<endl;


    char buf[5]={'d','e','f','g','h'};
    for(int i=0;i<5;i+=2)
    {
        fputc(buf[i],fd);
        putchar(buf[i+1]);
    }

    fflush(fd);
   int tmp =0;
 //if(ungetwc(101,fd)==EOF)
    cout<<"back fail"<<endl;
    fseek(fd,SEEK_SET,0);
   while( (tmp = fgetc(fd))!=EOF)
   {
        //cout<<"tem= "<<tmp<<endl;
       // if(tmp == 98 )ungetwc(tmp,fd);
       printf("tem = %d :%c\n",tmp,tmp);
   }
   fclose(fd);
  cout<<"====== ======"<<endl;
  FILE *fd1 = fopen("c.txt","a+");
  char buffer[1024];
  setbuffer(fd1,buffer,1024);
  if(fd1 ==NULL)
    {
        cout<<"open fail"<<endl;
    }
    fputs("test fputs\n",fd1);
   // sleep(20);
    //fseek(fd1,SEEK_SET,0);
    rewind(fd);
    char buf1[128] = {0};
    fgets(buf1,128,fd1);
    cout<<"buf = "<<buf1<<endl;
    fclose(fd1);
}
int main()
{
    cout<<"=====stdfilefwriteTest======"<<endl;
    stdfileTest();
    return 1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值