文件输入输出总结
1 基于文件指针的数据读写
基于文件指针的数据读写,通常为标准函数,在Windows与Linux下,均可以使用。
数据块读写
NAME fread, fwrite - binary stream input/output SYNOPSIS #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
DESCRIPTION The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the loca- tion given by ptr. The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the loca- tion given by ptr.
RETURN VALUE fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).
fread从文件流stream 中读取nmemb个元素,写到ptr指向的内存中,每个元素的大小为size个字节。
fwrite从ptr指向的内存中读取nmemb个元素,写到文件流stream中,每个元素size个字节。
所有的文件读写函数都从文件的当前读写点开始读写,读写完以后,当前读写点自动往后移动size*nmemb个字节。
字符串读写
SYNOPSIS
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream); int fputs(const char *s, FILE *stream); char *gets(char *s); 等同于 fgets(const char *s, int size, stdin); int puts(const char *s); 等同于 fputs(const char *s, stdout);
DESCRIPTION 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 new- line is read, it is stored into the buffer. A '\0' is stored after the last charac- ter in the buffer. gets() reads a line from stdin into the buffer pointed to by s until either a termi- nating newline or EOF, which it replaces with '\0'. No check for buffer overrun is performed (see BUGS below). fputs() writes the string s to stream, without its trailing '\0'. puts() writes the string s and a trailing newline to stdout.
gets()会忽略'\n',如果程序一执行,就按enter的话,字符串中存的就是'\0'。遇到错误或到文件结尾,返回NULL。
puts()会把'\0'换成\n输出。遇到错误返回EOF。
fgets()返回数组首地址,'\n'也存上,再加个'\0',遇到文件结尾返回NULL。遇到错误或到文件结尾,返回NULL。
fputs不会在行尾自动添加换行符。遇到错误返回EOF。
注意:从文件中读字符串,末尾都是会自动添加’\0’d的。
RETURN VALUE puts() and fputs() return a non-negative number on success, or EOF on error. gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.
格式化读写
SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...); //相当于fprintf(stdout,format,…); int scanf(const char *format, …);
int fprintf(FILE *stream, const char *format, ...); int fscanf(FILE *stream, const char *format, …);
int sprintf(char *str, const char *format, ...); int sscanf(char *str, const char *format, …);
以f开头的将格式化后的字符串写入到文件流stream中,或者从文件流stream中读取格式化后的字符串
以s开头的将格式化后的字符串写入到字符串str中,或者从字符串str中读取格式化后的字符串
对于写函数,返回写的字符个数(不包括‘\0’),遇到错误返回一个负数。
对于读函数,返回匹配的个数。遇到错误或者到达文件结尾,返回EOF。
注意:对于读函数,匹配字符串时会忽略空格,并且会在结尾加’\0’。
单个字符读写
RETURN VALUE fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error. fputc(), putc() and putchar() return the character written as an unsigned char cast to an int or EOF on error. puts() and fputs() return a non-negative number on success, or EOF on error.
2 基于文件描述符的数据读写
read与write函数是Linux系统调用,仅仅用于Linux系统。非缓冲。
注意
针对管道,read的返回值有如下3种情况:
1. 读取正常,返回读到的字符个数
2. 对方写端关闭,read返回0
3. 自己的读端关闭,read出错,返回-1。
NAME read - read from a file descriptor SYNOPSIS #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); DESCRIPTION read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified. RETURN VALUE On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
NAME write - write to a file descriptor SYNOPSIS #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. RETURN VALUE On success, the number of bytes written is returned (zero indicates nothing was writ- ten). On error, -1 is returned, and errno is set appropriately.
注意:用read读数据到数组中时,是不会自动在末尾加’\0 ’的!