Article directory
Preface
The standard c library is a set of function libraries for the application layer, 标准I/O库就是标准c库当中用于文件I/O操作相关的一套库函数
. To use the standard I/O library, you need to include the header file <stdio.h>.
1. The difference between standard I/O and file I/O
1. Standard I/O is 库函数
, and file I/O is 系统调用
.
2. Standard I/O is an encapsulation of file I/O.
3. Portability: Standard I/O compared to file I/O具有更好的可移植性
.
4. Better efficiency: Standard I/O is at 效率上高于文件I/O
. This is because standard I/O is buffered at the application layer (stdio buffer). File I/O is not buffered at the application layer, so standard I/O is more efficient.
2. FILE pointer and fopen() function
FILE pointer:
Standard I/O uses the FILE pointer as the file handle, 与文件I/O中的文件描述符相似
.
fopen() function - open a file
Standard I/O uses the fopen function to open a file:
FILE *fopen(const char *path,const char *mode);
Detailed explanation of parameters:
path: Points to the file path, which can be an absolute path or a relative path.
mode: Specifies the read and write permissions for the file, which is a string.
mode | illustrate | Corresponds to the flags parameter value of the open() function |
---|---|---|
r | Open file as read-only | O_RDONLY |
r+ | Open file for reading and writing | O_RDWR |
In | Open the file in write-only mode. If the file specified by the parameter path exists, the file length will be truncated to 0; if the specified file does not exist, the file will be created. | O_WRONLY | O_FRAME | O_TRUNC |
w+ | Open the file in readable and writable mode. If the file specified by the parameter path exists, the file length will be truncated to 0; if the specified file does not exist, the file will be created. | O_RDWR | O_FRAME | O_TRUNC |
a | Open the file for writing only, open for appending (writing at the end of the file), create the file if it does not exist | O_WRONLY | O_CREAT | O_APPEND |
a+ | Open the file for reading and writing, write in append mode (write at the end of the file), create the file if it does not exist | O_RDWR | O_CREAT | O_APPEND |
Memory points:
有带加号的就是以可读可写形式打开文件
, r/r+ has no special function, w/w+ can truncate the file length of existing content to 0 (O_TRUNC), a remember append, in the file Write at the end.
Return value:
A successful call returns a pointer to a FILE type object (FILE*), which is associated with the opened or created file. Subsequent standard I/O operations Will be done around the FILE pointer. If it fails, NULL is returned and errno is set to indicate the cause of the error.
Permissions for new files:
The fopen function has only two parameters, path and mode. There are no parameters to specify the permissions for new files. When the parameter mode takes the value "w, w+, a , a+", if the specified file does not exist, the file will be created, at this time 新建的文件有一个默认权限
, 其权限是0666(八进制)
.
fclose() function - close the file
Function prototype:
int fclose(FILE *stream);
3. Read files and write files
Reading files——fread() function
Function prototype:
size_t fread(void *ptr,size_t size,size_t nmemb,FILE*stream);
Detailed explanation of parameters:
The first parameter pointer ptr: points to the buffer;
The second parameter size: the size of a data packet/data item The byte size, rather than the total byte size of the data to be read;
The third parameter nmemb: the number of data items
The fourth parameter stream: FILE pointer, pointing to the file we want to read
Return value:
成功时返回读取到的数据项的数目
(not the actual number of bytes read, unless size is equal to 1); if发生错误或到达文件末尾
, 则fread()返回的值将小于参数nmemb
, at this time 无法区分是到达了文件末尾还是发生错误
, 需要使用ferror()或feof()函数
to judge.
Write files——fwrite() function
Function prototype:
size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE *stream);
Its parameters are roughly the same as those of the fread function and will not be described again.
Return value:
Call 成功的时候返回写入的数据项的数目
; if发生错误,则返回的值将会小于参数nmemb(或等于0)
It should be noted that, fread和fwrite发生错误的时候是不会设置errno值的,所以不用perror
.
Set the read and write position——fseek() function
Function prototype:
int fseek(FILE *stream,long offset,int whence);
has roughly the same function as lseek.
Detailed explanation of parameters:
stream: FILE pointer; offset: position offset; whence: set reference value (file head, file end, current pointing position )
Return value:
Returns 0 on success, -1 on failure, 与lseek返回值不一样
! A successful lseek will return the position offset in bytes from the beginning of the file.
Determine whether the file reaches the end of the file - feof() function
Function prototype:
int feof(FILE *stream);
If the file reaches the end of the file, return a non-zero value (true), otherwise return 0 (false).
Determine whether an error has occurred——ferror() function
Function prototype:
int ferror(FILE *stream);
Return value:
If an error occurs, it returns true, otherwise it returns false.
Clear flag——clearerr() function
Function prototype:
void clearerr(FILE *stream);
Function:
For example, when the file read-write pointer reaches the end of the file, 会设置标志位
end-of-file, the feof function passes this flag To determine whether the file has reached the end, 如果这个标志位不进行清除的话,就可能对之后的判断造成误判
, the clearerr function is to avoid this problem.
Usage example:
ret = fread(buf,1,11,f);
if(11>ret){
if(ferror(f)){
printf("fread error\n");
fclose(f);
return 1;
}
else {
if(feof(f)){
printf("fread end-of-file\n");
fclose(f);
return 1;
}
}
clearerr(f);
}