陈铭的专栏

书山有路勤为径,书海无涯苦作舟

linux 学习笔记之 Standard I/O Library
  1. Buffereing

            Three types of buffereing provided:

  • Fully buffered. Files residing on disk are normally fully buffered by the standard I/O library.
  • Line buffered. Line buffering is typically used on a system when it refers to a terminal: standard input and standard output.
  • Unbuffered. It makes the data output as soon as possible. The standard error stream, for example, is normally unbuffered. This is so that any error messages are displayed as quickly as possible.
    void setbuf(FILE *fp, char *buf);
    void setvbuf(FILE *fp, char *buf, int mode, size_t size);
    This function must call after the stream has been opened but before any other operation is performed on the stream.
  • With setbuf, we can turn buffering on or off. To enable buffering, buf must point to a buffer of length BUFSIZ. To disable buffering, we set buf to NULL.
  • With setvbuf, We specify exactuly which type of buffering we want. This is done with the mode argument: _IOFBUF -> fully buffered, _IOLBUF -> line buffered, _IONBUF -> unbuffered. The size argument equal the length of the buf.
  • In general, we should let the system choose the bufer size and automatically allocate the buffer. When we do this, the standard I/O library automatically release the buffer when we close the stream.
    int fflush(FILE *fp)
    This function causes any unwritten data for stream to be passed to the kernel. As a special, if the fp is NULL, this function causes alll output streams to be flushed.

      2.    Opening Steam

FILE * fopen(const char *pathname, const char * type);
FILE 
* freopen(const char *pathname, const char *type, FILE *fp);
FILE 
* fdopen(int fd, const char *tpye);
  • The freopen function opens a specified file on a specified steam, closeing the stream first if it is already open. We usually use it link a standard I/O to a file as freopen("filename","r",stdin) this function equal dup2(fd,0).
  • The fdopen function takes an existing file descriptor, which we could obtain form the open, dup, dup2, fcntl, pipe, socket, socketpair or accept functions, and associates a standard I/O steam with the descriptor. The descriptor has already been opened, so opening for write does not truncate the file. Also, The standard I/O append mode cannot create  the file.

       When a file is opened for reading or writing, the following restrictions apply:

  • Output cannot be directly followed by input without an interveing fflush, fseek, fsetops or rewind.
  • Input cannot be directly followed by output without an interveing fseek, fsetpos or rewind, or an input opereation that encounters and end of file.
  • Note that if a new file is created by specifying a type of either w or a, we are not able to specify the file's access permission bits, as we were able to de witch the open function and the create function.

        3.    Reading and Writing a Stream

              Once we open a stream, we can choose form among three types of unformatted I/O:

  • Character-at-a-time I/O
    int getc(FILE *fp);
    int fgetc(FILE *fp);
    int getchar(void);
      The defferents bewteen getc and fgets is that getc can be a marco, whereas fgets cannot be implemented as a macro. But the detail is depond on your system, in my system(Fedora 7) getc is a macro, so in my system getc function is faster than fgetc function.
  • Line-at-a-time I/O
    char * fgets(char *buf, int n, FILE * fp);
    char * fputs(const char *buf, FILE *fp);
    We'd better don't use gets and puts function beacuse these my lead your program overflow.
  • Direct I/O
    size_t fread(void * ptr, size_t size, size_t nobj, FILE *fp);
    size_t fwrite(
    const void *fp, sizt_t size, size_t nobj, FILE *fp);
    These functions have to common uses: read or write a binary arrary, read or write a structure.

       4.    Implementation Details

            Each standard I/O steam has a associated file descriptor, and we can obtain the descriptor for a stream by calling fileno.

int fileno(FILE *fp);
           We need this function if we want to call the dup or fcntl functions, for example.

 

 

 

 

 

 

 

阅读更多
个人分类: Linux
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

linux 学习笔记之 Standard I/O Library

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭