Linux 标准输入输出详解

来源:互联网 发布:linux grep -ef|ps 编辑:程序博客网 时间:2024/05/19 05:40

linux高级 标准输入输出


----------------------------------------------------------------------------------------

1、 标准输入输出与普通的输入输出的区别

-----------------------------------------------------
   
        普通标准输入输出有没有缓存区;标准输入输出有缓存区;

------------------------------------------------------------------------

2、标准i/o缓存分类

-----------------------------------------------------
      全缓存
               当填满i/o缓存后才能进行i/o操作
      行缓存 
               当输入和输出遇到新行符\n时,进行i/o操作
      不缓存
               不经过缓存区,直接输入到屏幕上
 setbuf() 设置缓存区  不用系统定义的缓存区,用自己定义的缓存区
void setbuf(FILE *stream, char *buf); int setvbuf(FILE *stream, char *buf, int mode, size_t size);
fflush可强制刷新一个流,不能用来刷新标准输入。
fflush(stdout);
 -----------------------------------------------------------------------------------------------------

 3、字符的标准输入输出

------------------------------------------------------------------------------------------------------
int fgetc(FILE *stream); 
    用于从文件流中获取字符,此函数成功返回一个非0的int类型的数,失败返回一个NULL
   用法:
while((ret = fgetc(fp)) != EOF)   {     fputc(ret,stdout); }
 
int fputc(int c, FILE *stream);
 介绍:
    fputc() writes the character c, cast to an unsigned char, to stream.
    将以指定的字符c强转成unsigned int 输出到stream
    以上两个函数的返回:如成功则为下一个字符,如已处文件末尾或出错则为EOF,为了区分,必须调用ferror()和feof();
用法如下:

while((ret = fgetc(fp)) != EOF){    fputc(ret,stdout); }    if(feof(fp)) 文件末尾;    printf("to end\n");    if(ferror(fp))文件不正常退出    printf("to error\n");
 -----------------------------------------------------------------------------------------------------

 4、字符串的标准输入输出

-----------------------------------------------------------------------------------------------------
 char *fgets(char *s, int size, FILE *stream);
    函数的意思是从stream中读取size个字节输出到s中,注意两个参数都是指针
    此函数实际有效获取的字符只有size-1个;
    函数有四种结束的情况:
     第一:遇到换行符结束
     第二:文件读完结束
     第三:达到size
        第四:文件有突发情况结束;不正常退出;
    函数成功返回指针s,FILE *stream文件读取结束了也是返回null,所以我们要判断文件是结束了还是 非正常退出了
    有两个函数,
    int feof(FILE *stream);     int ferror(FILE *stream);
   if(feof(fp)) 文件流读完了,退出;
         printf("to end\n");
   if(ferror(fp))文件不正常退出
         printf("to error\n");

int fputs(const char *s, FILE *stream);
    将一个以null符终止的字符串写到指定的流
    fputs() writes the string s to stream, without its trailing '\0'.
    遇到‘\0’结束;所以gets不能操作二进制文件;
    成功返回为非0值,失败返回则为EOF;

 ------------------------------------------------------------------------------------------------------------

  5、字符块的读写

------------------------------------------------------------------------------------------------------------
 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
函数意思:     
     nmemb读的次数;size一次读的字节,从FILE *stream读到ptr 
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
     从ptr写到FILE *stream
 注意:可以操作二进制文件;
 
--- -----------------------------------------------------------------------------------------------------------

6、位置有关的函数

---------------------------------------------------------------------------------------------------------------
int fseek(FILE *stream, long offset, int whence);
   参数:
     用户设定stream流的文件位置指示,调用成功返回0,若出错则为-1
     offset偏移量,相对int whence的偏移量;whence参数;SEEK_SET/SEEK_CUR/SEEK_END
     long ftell(FILE *stream);   //用于读取当前文件的位置,调用成功则为当前文件位置指示,出错,则为-1;
     void rewind(FILE *stream);//用于流的文件位置指示问文件开头开始,该函数调用成功无返回值
     rewind()等价于()fseek(stream,0L,SEEK_SET)

int fgetpos(FILE *stream, fpos_t *pos);int fsetpos(FILE *stream, fpos_t *pos);
    两个函数返回:若成功则为0;若出错则为非0
   fgetpos()将文件位置指示器的当前值存入由pos指向的对象中,在以后调用fsetpos时,可以使用次值将流重新定位至该位置

----------------------------------------------------------------------------------------------------------------------

7、与文件有关的函数

----------------------------------------------------------------------------------------------------------------------
FILE *fopen(const char *path, const char *mode);
参数:     
     路径:argv[1],像“1.c”这样的文件
     模式 :
      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 beginning 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 endof 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.
                 以上四个都有创建文件的功能
 int fclose(FILE *fp);   

原创粉丝点击