EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一直在看书,但不知道是否可以完全理解,所以就写在这了,当是对知识的总结。最近再看Unix环境下的编程,读了一本经典Standard I/O Library,初次接触,还是有些不是很明白的地方,暂且把知识点列在这吧。

 

第五章 标准IO库:

5.1流和FILE对象

Standard I/O file streams can be used with single-byte and multibyte ("wide") character sets.

可以通过fwide()函数设置mode。

 

#include <stdio.h>
#include <wchar.h>
int fwide(FILE *fp, int mode);

Returns: positive if stream is wide-oriented,
negative if stream is byte-oriented,
or 0 if stream has no orientation

 

5.2 <stdio.h>设置三种预置流file指针stdin, stdout, and stderr分别指向三种预置流file descriptor:STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO

 

5.3缓冲buffering

设置缓冲区是为了减少read和write的次数,提高系统效率。

分为三种缓冲:

1)fulled buffering:当缓冲区满时,自动flush数据。

2) lined buffering: 每当输入或输出遇到换行符时,自动flush数据。

3)unbuffering: 每当输入一个字符,自动flush数据。

Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device.

Standard error is never fully buffered.

下面的两个函数可以设置缓冲区大小,但是都需要在已经打开file descriptor设置

 

#include <stdio.h>
void setbuf(FILE *restrict fp, char *restrict buf);
int setvbuf(FILE *restrict fp, char *restrict buf,
int mode, size_t size);

Returns: 0 if OK, nonzero on error

 

setvbuf mode argument:
_IOFBF fully buffered
_IOLBF line buffered
_IONBF unbuffered

flush()可以用于flush数据,

#include <stdio.h>
int fflush(FILE *fp);


Returns: 0 if OK, EOF on error

如果arg为空时,那么flush所有数据。

 

 

5.4 打开/关闭流

打开流有以下函数

 

#include <stdio.h>
FILE *fopen(const char *restrict pathname, const
char *restrict type);
FILE *freopen(const char *restrict pathname, const
char *restrict type, FILE *restrict fp);
FILE *fdopen(int filedes, const char *type);

 

前两个函数打开一个path,其中第2个函数可以重置byte-orientation,关闭一打开的流,然后在重新打开。

第三个函数用于打开file desciptor。

关闭流的函数:

#include <stdio.h>
int fclose(FILE *fp);

 

Returns: 0 if OK, EOF on error

#include <stdio.h>
int fclose(FILE *fp);

Returns: 0 if OK, EOF on error

 

 

5.7读写流

读写流可以分为三种模式

1)一次一个字符

 

#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);

All three return: next character if OK, EOF on end of file or error
#include <stdio.h>
int ferror(FILE *fp);
int feof(FILE *fp);

Both return: nonzero (true) if condition is true, 0 (false) otherwise
void clearerr(FILE *fp);
//push back 字符到流中
#include <stdio.h>
int ungetc(int c, FILE *fp);

Returns: c if OK, EOF on error

 

输出:

 

#include <stdio.h>
int putc(int c, FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c);

 

2)一次一行

 

#include <stdio.h>
char *fgets(char *restrict buf, int n, FILE
*restrict fp);
char *gets(char *buf);

 

输出:

#include <stdio.h>
int fputs(const char *restrict str, FILE *restrict
fp);
int puts(const char *str);

 

puts()函数并不安全,可能over缓冲区

3)一次一个对象(固定大小缓冲区)

 

#include <stdio.h>
size_t fread(void *restrict ptr, size_t size,
size_t nobj, FILE *restrict fp);
size_t fwrite(const void *restrict ptr, size_t
size, size_t nobj, FILE *restrict fp);

 

 

5.10 定位文件指针

#include <stdio.h>
long ftell(FILE *fp);

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

Returns: 0 if OK, nonzero on error

 

5.11格式化IO

#include <stdio.h>
int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fp, const char
 *restrict format, ...);


Both return: number of characters output if OK, negative value if output error

The printf function writes to the standard output, fprintf writes to the specified stream, and sprintf places the formatted characters in the array buf. The sprintf function automatically appends a null byte at the end of the array, but this null byte is not included in the return value.

 

以上为我所总结,知识有限,错误之处,还请纠正

posted on 2010-11-09 00:41  Eric-Yang  阅读(207)  评论(0编辑  收藏  举报