C语言——stdio.h

int fgetc(FILE * stream); get character from stream
    返回流中的一个字符,并以int的类型返回,如果碰到文件的结尾,或者一个错误发生,函数返回EOF,同时相应的错误或eof指示器被设置,可以用ferror或者feof来检查发生一个错误,或者到达了文件的末尾


int getc(FILE * stream); get character from stream;

     功能和fgetc一样,只是getc用宏来实现,因此当传递参数时,不应该是一个表达式,以防止潜在的副作用

 

int getchar(void); get character from stdin;

     从标准输入中读取下一个字符,等价于getc函数中奖参数设置为标准输入。返回值跟fgetc,getc一样。


int putchar(int character); write character to stdout

     将字符输出到标准输出中,如果没有错误发生,返回相同的字符,如果错误发生,则返回EOF,同时错误指示器被设置。


char * fgets(char * str, int num, FILE * stream); get string from stream

    从流中读字符串,将读到的num-1个字符存放到str所指向的缓冲中。当碰到换行或者EOF时,读结束。但是换行被作为一个有效的字符被读进了缓冲,同时自动在末尾加上一个NULL。通常num等于缓冲的大小。无错误时,函数返回指向字符串的指针,如果有错误发生则返回null。可以用ferror或feof来检查是错误发生还是到达了文件结尾。


char * gets(char * str); get string from stdin

    从标准输入读字符并将其存放到str中,直到碰到换行\n或者EOF,但是换行符并不读入到str中,这跟fgets不同。而且gets不知道要读多少字符,要特别小心以防止buffer overflow


int fputs(const char* str, FILE * stream); write string to stream

    将str所指向的字符串写入到流中,直到字符串中碰到一个null,最后的null不会被写入到流中。成功返回一个非负的整数,错误返回EOF


int puts(const char * str); write string to stdout

将字符串写到标准输出中,并附加一个换行符\n,当第一次碰到字符串中的null时,拷贝停止,null不会被写入到stdout中。
功能跟fputs(str, stdout)类似,但是fputs不附加一个换行符。


int fputc(int character, FILE * stream); write character to stream;

    将字符写入到流中,并移动指示器的位置。成功会返回写入的字符,负责返回EOF。


int putc(int character, FILE *stream); write character to stream;

    功能跟fputc一样,只是putc用宏来实现。因此要防止潜在的错误发生;


int ungetc(int character, FILE * stream); unget character from stream

    将一个字符虚拟的放入到输入流中,位置是上次读字符的位置。文件指示器倒退到前一个位置,因此这个被虚拟放入的字符被下一次的读操作所读到。只所以称之为虚拟放入,是因为和这个流相关联的文件的内容不会有任何的变化,因此这个操作只影响到下一次的读操作。要虚拟放入的字符可以跟上一次读的字符相同或者不相同。可以多个字符被虚拟放入。
    如果设置了eof,调用这个函数后会将eof清除。fseek,fsetpos,rewind的调用会丢弃放入到流中的虚拟字符。如果要放入的是eof,则操作会失败,输入流不会发生变化。
直接的输入和输出


size_t fread(void * ptr, size_t size, size_t count, FILE * stream); read block of data from stream

    read an array of count elements, each one with a size of bytes, from the stream and stores them in the block of memory specified by ptr.

    the positon indicator of the stream is advanced by the total amount of bytes read.the total amount of bytes read if successful is (size * count);返回值为读到的element数,如果跟count不相等,则返回错误。


size_t fwrite(count void * ptr, size_t size, size_t count, FILE * stream); write block of data to stream

    将内存中的数据写入到流中,返回值为写入的element数,如果不等于count则错误。

流中的位置指示


int fgetpos(FILE * stream, fpos_t position); get current position in stream

    得到流目前的指示器,并将其存放到position所指向的对象中,这个只供fsetpos函数使用。成功返回0,否则返回一个错误。


int fsetpos(FILE * stream, const fpos_t pos); set position indicator of stream

    改变文件指示器,eof被清除,同时ungetc函数的效果也被清除。on streams open for update(read + write), a call to fsetpos allows to switch between reading and writing.成功返回0,否则返回非零值,同时设置全局的errno,可以用perro来翻译这个值。


int fseek(FILE * stream, long int offset, int origin); repositon stream position indicator;

     用offset和origin来共同设置指示器,会清除eof和ungetc函数的效果。在文本文件中使用offset,而不是0或者ftell函数的返回值时,在某些平台上会有一些格式的转换,导致不可预料的位置指示。offset表示number of bytes。SEEK_SET SEEK_CUR SEEK_END


long int ftell(FILE * stream); get current positon in stream

    返回目前指示器的值,是个长整型。对于二进制文件,返回从开始到指示器的字节数,对于文本文件不能保证返回的值是正确的字节数,但是返回值仍然可以用在fseek的offset变量中。发生错误会返回-1L,同时设置全局变量errno,可以用perror来扑捉;


void rewind(FILE * stream);set positon indicator to the beginning

    等价于fseek(stream, 0L, SEEK_SET),除了rewind会清除错误指示器。
错误处理函数


void clearerr(FILE) ; clear error indicator

    reset both error and eof indicator of the stream.通过调用rewind,fseek,fsetpos函数会重新设置指示器
int feof(FILE * stream); check end-of-file indicator
    检查是否eof被设置,也就是是否到达文件的末尾,如果是则返回非零值,否则返回0.如果eof被设置,则对stream的进一步操作会失败,除非使用rewind,fseek,fsetpos函数进行重新设置。


int ferror(FILE * stream); check error indicator

    检查是否有错误指示器被设置,如果是则返回非零值,否则返回0.


void perror(const char * str); print error message

    将全局变量errono的值翻译成字符串,并打印。
常量
EOF

    是个非整数,通常有函数调用错误,或者到文件尾时返回。



来源:http://blog.sina.com.cn/s/blog_4a97718101016rd5.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值