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 witha 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
是个非整数,通常有函数调用错误,或者到文件尾时返回。
C语言——stdio.h的更多相关文章
- C语言中头文件<stdio.h>中的#ifndef _STDIO_H_
先了解这里的相关知识:http://www.cnblogs.com/stemon/p/4000468.html 头文件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都in ...
- c语言输入与输出库函数#include<stdio.h>
last modified: 2010-05-28 输入与输出<stdio.h> 头文件<stdio.h>定义了用于输入和输出的函数.类型和宏.最重要的类型是用于声明文件指针的 ...
- include<stdio.h> 和include<iostream.h>的区别
stdio 是C标准库里面的函数库 对应的基本都是标准输入输出等等C语言常用库的定义iostream是C++标准库的头定义, 对应的基本上是C++的输入输出相关库定义开发C程序用Stdio, C++用 ...
- stdio.h及cstdio的区别
2013-07-04 16:45:19 找了很多资料,没有说的很明白的,下面是老外的一篇文章,解释的比较清楚,后面给出翻译. Clarifying stdio.h versus cstdio 转自:h ...
- 学习第一个头文件stdio.h
使用标准输入输出库函数时要用到 “stdio.h”文件,因此源文件开头应有以下预编译命令: #include<stdio.h> stdio是standard input&outup ...
- 走进C标准库(3)——"stdio.h"中的getc和ungetc
接前文. 再来看看getc和ungetc的实现.在看这两个函数的实现之前,我们先来想一想这两个函数分别需要做的工作. int getc(FILE *stream) 说明:函数getc从stream指向 ...
- 走进C标准库(2)——"stdio.h"中的fopen函数
其他的库文件看起来没有什么实现层面的知识可以探究的,所以,直接来看stdio.h. 1.茶余饭后的杂谈,有趣的历史 在过去的几十年中,独立于设备的输入输出模型得到了飞速的发展,标准C从这个改善的模型中 ...
- stdio.h cstdio的区别
stdio.h cstdio string.h cstring math.h cmath .h是c语言的习惯,在c++中,替换为在前面加个c
- #include<stdio.h> #include "stdio.h"
https://baike.baidu.com/item/#include <stdio.h> #include <stdio.h> 编辑 #include<stdio. ...
随机推荐
- linux中的目录和文件的统计
ls 目录 | wc -l find ./ -type d | wc -l //查找目录个数 find ./ -type f | wc -l ...
- Nginx(四)-- 配置文件之location
1.location的作用 location主要做定位功能,根据uri来进行不同的定位. 2.location的语法 location [=|~|~*|^~] /uri/ { …} = 开头表示精确匹 ...
- Oracle函数的使用
日期转换to_date insert into test (birthday,name) values (to_date('2016-03-12','yyyy-mm-dd'),'zy'); to_ch ...
- sql语句如何删除最后一条和第一条信息
这是先前建好的SQL数据库中的test表, sql语句: delete a from test a,(select max(id) id from test) b where a.id = b.id ...
- php学习十一:组合
我们在类当中我往往会用到一些对象,此时的继承就无法满足我们的需求,这个时候我们需要用到组合.继承如果是is..a的关系,那么组合就是has...a的关系,直接在本类里面声明即可,不过声明的是一个对象 ...
- 详谈redis优化配置和redis.conf
1. Redis.conf 配置参数: #是否作为守护进程运行 daemonize yes #如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid pidfile redi ...
- 【PHP】数字补零的两种方法
在php中有两个函数,能够实现数字补零, str_pad() sprintf() 函数1 : str_pad 顾名思义这个函数是针对字符串来说的这个可以对指定的字符串填补任何其它的字符串 例如:str ...
- 【markdown】使用 js 实现自己得markdown 网页编辑器
首先从这里下载其浏览器版: https://github.com/evilstreak/markdown-js/releases 解压缩后在其js文件同目录下新建一个网页进行测试,代码如下: < ...
- diamond types are not supported at this language level
在intellij导入git项目之后出现 diamond types are not supported at this language level错误 或者String等报错 File->P ...
- LeetCode——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...