linux 学习笔记之 Standard I/O Library

原创 2007年09月25日 22:14:00
  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);
* freopen(const char *pathname, const char *type, FILE *fp);
* 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.









  • Jacie_chaochao
  • Jacie_chaochao
  • 2015年11月30日 21:47
  • 1457

Vivado 约束条件出错

 When generating a bitstream, the following error messages occur: ERROR: [Drc 23-20] Rule violat...
  • LovingDuo
  • LovingDuo
  • 2016年07月01日 09:50
  • 2509

使用vivado进行逻辑开发时,进行到Generate Bitstream时报错

使用vivado进行逻辑开发时,进行到Generate Bitstream时报错,如下:   [Drc 23-20] Rule violation (NSTD-1) Unspecified...
  • hemmingway
  • hemmingway
  • 2014年09月29日 14:55
  • 14508

【重要】Linux I/O模型

socket阻塞与非阻塞,同步与异步、I/O模型 分类: c/c++ socket网络编程2012-04-12 16:35 32926人阅读 评论(22) 收藏 举报 socket...
  • chenchong_219
  • chenchong_219
  • 2014年06月29日 15:10
  • 1622

Java I/O学习(附实例和详解)

原文地址: 一、Java I/O类结构以及流的基本概念 在阅读Java I/O的...
  • tuzongxun
  • tuzongxun
  • 2016年03月08日 18:10
  • 707

Linux I/O端口与I/O内存

一、IO端口访问  1、直接使用IO端口操作函数    1)在设备打开或驱动模块被加载时申请IO端口区域,之后使用inb(),outb()等进行端口访问,最后在设备关闭或驱动被卸载时释放IO端口范...
  • luckywang1103
  • luckywang1103
  • 2013年11月29日 15:40
  • 847

Linux 下I/O多路复用总结

  • hsy12342611
  • hsy12342611
  • 2016年04月07日 15:45
  • 2688

Linux 网络 I/O 模型简介(图文)

1、介绍 Linux 的内核将所有外部设备都看做一个文件来操作(一切皆文件),对一个文件的读写操作会调用内核提供的系统命令,返回一个file descriptor(fd,文件描述符)。而对一...
  • anxpp
  • anxpp
  • 2016年05月26日 01:12
  • 19321

Linux 磁盘I/O读写速度检测

概述读取速度可以使用命令:hdparm –t 设备名(/dev/sda1)写入速度使用命令:time dd if=/dev/zero of=/tmp/test.dat bs=1G count=1测试...
  • yangshangwei
  • yangshangwei
  • 2017年01月21日 08:33
  • 2490


  • xygl2009
  • xygl2009
  • 2015年07月15日 20:56
  • 1818
您举报文章:linux 学习笔记之 Standard I/O Library