本博客主要参照了李慧琴老师的Linux课程
文章目录
标准IO
概念
标准I/O是ANSI C建立的一个标准I/O模型,是一个标准函数包和stdio.h头文件中的定义,不依赖系统内核,所以移植性强。又称为高级磁盘I/O,遵循ANSI C相关标准。只要开发环境中有标准I/O库,标准I/O就可以使用。(Linux 中使用的是glibc,它是标准C库的超集。不仅包含ANSI C中定义的函数,还包括POSIX标准中定义的函数。因此,Linux 下既可以使用标准I/O,也可以使用文件I/O)。标准I/O库处理很多细节,例如缓冲分配,以优化长度执行I/O等。
换句话说,只要有相关支持,就可以使用标准IO,限制小
相关函数
- fopen() fclose()
- fgetc() fputc()
- fgets() fputs()
- fread() fwrite()
- printf() scanf()
- fseek() ftell()
- getline()
fopen() fclose()
fopen()
查询man手册技巧:第一步要了解函数的功能,第二步查看函数原型,了解返回值类型,函数各个参数的含义等,第三步,看函数的返回值类型。这样就可以基本了解这个函数的用法了
故,查询 man 手册可知
NAME
fopen, fdopen, freopen - stream open
functions
//解释:这几个函数的作用都是打开一个stream
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *pathname, const char
*mode);
解释:第一个参数指定要打开的文件,第二个指明打开文件的权
限是什么,返回值是FILE *,如果要接收,使用FILE * 类型的指
针来接收。大家可以看到,两个函数都有const,说明用户传来的数
据不会有任何改变
DESCRIPTION
The fopen() function opens the file whose
name is the string pointed to by pathname and
associates a stream with it.
翻译: fopen()函数的作用是根据第一个参数——路径名打开
文件,并将一个流与之关联
RETURN VALUE
Upon successful completion fopen(), fdopen()
and freopen() return a FILE pointer. Otherwise,
NULL is returned and errno is set to indicate
the error.
解释:三个函数,成功都返回文件指针,如果失败,返回NULL(空
指针)并且设置errno(这是一个宏,[errno详细信息](https://blog.csdn.net/chenlycly/article/details/20489483))
有关mode参数的详细使用
The argument mode points to a string beginning
with one of the following sequences (possibly
followed by additional characters, as described
below):
r Open text file for reading. The
stream is positioned at the beginning of the
file.
//以只读方式打开文件,stream流定位到文件开始处
r+ Open for reading and writing. The
stream is positioned at the beginning of the
file.
//以读写的方式打开文件,stream流定位到文件开始处
w Truncate file to zero length or
create text file for writing. The stream is
positioned at the beginning of the file.
//有则清空,无则以只写形式创建,定位开始处
w+ Open for reading and writing. The
file is created if it does not exist, otherwise
it is truncated. The stream is positioned at the
beginning of the file.
//有则清空,无则以读写形式创建,定位开始处
a Open for appending (writing at
end of file). The file is created if it does
not exist. The stream is positioned at the end
of the file.
//以追加只写的形式打开文件,写到文件的末尾处,如果文件不存在,
//会被创建,stream流指向文件的末尾处
a+ Open for reading and appending (writing at
end of file). The file is created if it does
not exist. The initial file position for reading
is at the beginning of the file, but output is
always appended to the end of the file.
//以追加读写的形式打开文件,写到文件的末尾处,如果文件不存在,会被创建
//但此处有个问题,写的时候在文件末尾处,那么怎么读呢?所以
//a+操作的文件指针指向哪里,取决于你的下一步操作
fclose()
此函数与fopen()函数相对应,free操作被封装在此函数中
NAME
fclose - close a stream //关闭一个流
SYNOPSIS
#include <stdio.h>
int fclose(FILE *stream);
//参数是通过fopen()函数所打开的文件
fgetc() fputc()
字符输入输出的函数
fgetc()
NAME
fgetc,fgets,getc,getchar,ungetc-input of
characters and strings
SYNOPSIS
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
//这两个函数,返回值和参数都是一样的,但是也有区别,fgetc被定义成函数使用,而getc被定义成宏来使用
char *fgets(char *s,int size,FILE *stream);
//不指定终端,返回值的类型是unsigned char 型,为了防止出错,定义成int型
int getchar(void);
int ungetc(int c, FILE *stream);
DESCRIPTION
fgetc() reads the next character from
stream and returns it as an unsigned char cast to
an int, or EOF on end of file or error.
getc() is equivalent to fgetc() except that
it may be implemented as a macro which evaluates
stream more than once.
getchar() is equivalent to getc(stdin).
fgets() reads in at most one less than
size characters from stream and stores them into
the buffer pointed to by s. Reading stops after
an EOF or a newline. If a newline is read, it is
stored into the buffer. A terminating null
byte ('\0') is stored after the last character
in the buffer.
ungetc() pushes c back to stream, cast
to unsigned char, where it is available for
subsequent read operations. Pushed-back characters
will be returned in reverse order; only one
pushback is guaranteed.
Calls to the functions described here
can be mixed with each other and with calls to
other input
functions from the stdio library for the
same input stream.
fputc()
NAME
fputc, fputs, putc, putchar, puts - output of characters and strings
SYNOPSIS
#include <stdio.h>
int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
int puts(const char *s);
fgets() fputs()
字符串相关的函数
二者均在上一对函数的man手册中有所呈现,请参考上文
fgets(* ,size , stream );
有可能有两种可能造成这个函数的正常结束
一、读到了 size-1 个有效字节
二、读到了 \n
假如要读取'abcd',需要读取几次?
abcd
1 ->a b c d '\0'
2 ->'\n' '\0' //这个文件需读两次,不能忽略 ‘\n'
fread() fwrite()
NAME
fread, fwrite - binary stream input/output
SYNOPSIS
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
//从sream中读取内容,放到ptr中,ptr这块地址的大小一共nmemb个对象,每个对象有size大小
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
DESCRIPTION
The function fread() reads nmemb items of data, each size bytes long, from the stream pointed to by
stream, storing them at the location given by ptr.
The function fwrite() writes nmemb items of data, each size bytes long, to the stream pointed to by
stream, obtaining them from the location given by ptr.
printf() scanf()
这对函数可能是大家经常用的函数了,在这里不多做介绍