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);
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.

 

 

 

 

 

 

 

相关文章推荐

《APUE》chapter 5 Standard I/O libary 学习笔记(加上自己的代码)

Standard I/O libary 学习笔记+code

Python Tutoral 学习笔记7 --Standard Library

1.The os module provides dozens of functions for interacting with the operating syste. 2.For da...

JAVA JDK学习笔记i\o部分

  • 2012年08月05日 21:38
  • 565KB
  • 下载

java_I\O学习笔记

  • 2012年10月19日 14:40
  • 440KB
  • 下载

《Linux内核设计与实现》学习笔记——I/O调度算法

I/O调度子系统用于调度来自多个进程对块设备的I/O请求。电梯调度 首先,如果队列中已存在一个对相邻磁盘扇区操作的请求,那么新请求将和这个已经存在的请求合并为一个请求。 2.如果队列中存在一个驻留时...

Linux系统编程学习笔记(1)-文件的I/O操作

概述在Linux系统下,通常以一个非负整数来代指一个打开的文件,这些文件可以包括终端,socket,设备,普通文件等等。规定的三个标准的文件描述符为0,1,2下面分别介绍(在交互式shell中,这些文...

Linux内核设计与实现 学习笔记(4)块I/O层

主要内容: 块设备简介内核访问块设备的方法内核I/O调度程序   1. 块设备简介 I/O设备主要有2类: 字符设备:只能顺序读写设备中的内容,比如 串口设备,键盘块设备:能够随机读写设备中...

Linux系统学习笔记:高级I/O

本篇总结一些高级的I/O操作,包括记录锁、I/O多路转接、存储映射I/O等。 Contents 记录锁I/O多路转接read和write函数的变种存储映射I/O 记录...

Linux学习笔记1——不带缓冲的文件I/O函数

1,OPEN_MAX 限制名OPEN_MAX代表每个进程最大打开的文件数。 注意,这个最大数是一个运行时限制。也就是说,对于一个系统而言,它可能并不是一个一成不变的值;换言之,我们也不能把OPEN_M...

linux网络编程学习笔记之六 -----I/O多路复用服务端

多进程和多线程的目的是在于最大限度地利用CPU资源,当mou
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:linux 学习笔记之 Standard I/O Library
举报原因:
原因补充:

(最多只允许输入30个字)