IO标准库和系统调用接口

标准库接口

fopen/fread/fwrite/fseek/fclose

#include <stdio.h>

FILE *fopen(const char *path, const char *mode);
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);
int fseek(FILE *stream, long offset, int whence);
int fclose(FILE *stream);

  • FILE *fopen(const char *path, const char *mode);
    #include <stdio.h>

description:
The fopen() function opens the file whose name is the string pointed
to by pathname and associates a stream with it.

The argument mode points to a string beginning with one of the
following sequences (possibly followed by additional characters, as
described below):

made:
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.

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.

a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. Output is always
appended to the end of the file. POSIX is silent on what the
initial read position is when using this mode. For glibc, the
initial file position for reading is at the beginning of the
file, but for Android/BSD/MacOS, the initial file position for
reading is at the end of the file.

  • size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    #include <stdio.h>

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.
return value:
On success, fread() and fwrite() return the number of items read or
written. This number equals the number of bytes transferred only
when size is 1. If an error occurs, or the end of the file is
reached, the return value is a short item count (or zero).
fread() does not distinguish between end-of-file and error, and
callers must use feof(3) and ferror(3) to determine which occurred.

  • size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
    #include <stdio.h>

description:
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.
return value:
On success, fread() and fwrite() return the number of items read or
written. This number equals the number of bytes transferred only
when size is 1. If an error occurs, or the end of the file is
reached, the return value is a short item count (or zero).

  • int fseek(FILE *stream, long offset, int whence);
    #include <stdio.h>

description:
The fseek() function sets the file position indicator for the stream
pointed to by stream. The new position, measured in bytes, is
obtained by adding offset bytes to the position specified by whence.
If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
relative to the start of the file, the current position indicator, or
end-of-file, respectively. A successful call to the fseek() function
clears the end-of-file indicator for the stream and undoes any
effects of the ungetc(3) function on the same stream.
return value:
successful completion,
fgetpos(), fseek(), fsetpos() return 0, and ftell() returns the
current offset. Otherwise, -1 is returned and errno is set to
indicate the error.

  • int fclose(FILE *stream);
    #include <stdio.h>

description:
The fclose() function flushes the stream pointed to by stream
(writing any buffered output data using fflush(3)) and closes the
underlying file descriptor.
return value:
Upon successful completion, 0 is returned. Otherwise, EOF is
returned and errno is set to indicate the error. In either case, any
further access (including another call to fclose()) to the stream
results in undefined behavior.

fgets/printf/sprintf(buffer)/snprintf(buffer)/fprintf(file)

char *fgets(char *restrict s, int n, FILE *restrict stream);
int sprintf(char *restrict s, const char *restrict format, …);
int snprintf(char *str, size_t size, const char *format, …);
int fprintf(FILE *stream, const char *format, …);

  • char *fgets(char *restrict s, int n, FILE *restrict stream);
    #include <stdio.h>

description:
The functionality described on this reference page is aligned with
the ISO C standard. Any conflict between the requirements described
here and the ISO C standard is unintentional. This volume of
POSIX.1‐2008 defers to the ISO C standard.
The fgets() function shall read bytes from stream into the array
pointed to by s, until n−1 bytes are read, or a is read and
transferred to s, or an end-of-file condition is encountered. The
string is then terminated with a null byte.
The fgets() function may mark the last data access timestamp of the
file associated with stream for update. The last data access
timestamp shall be marked for update by the first successful
execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(),
getdelim(), getline(), gets(), or scanf() using stream that returns
data not supplied by a prior call to ungetc().
return value:
Upon successful completion, fgets() shall return s. If the stream is
at end-of-file, the end-of-file indicator for the stream shall be set
and fgets() shall return a null pointer. If a read error occurs, the
error indicator for the stream shall be set, fgets() shall return a
null pointer, and shall set errno to indicate the error.

  • int sprintf(char *restrict s, const char *restrict format, …);
    #include <stdio.h>

description:
sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.
return value:
Upon successful return, these functions return the number of characters printed
(excluding the null byte used to end output to strings).

  • int snprintf(char *str, size_t size, const char *format, …);
    #include <stdio.h>

description:
The functions snprintf() and vsnprintf() write at most size bytes (including the
terminating null byte (’\0’)) to str.
the functions printf(), fprintf(), sprintf(), snprintf(), respectively, except
that they are called with a va_list instead of a variable number of arguments.
These functions do not call the va_end macro. Because they invoke the va_arg
macro, the value of ap is undefined after the call.
return value:
The functions snprintf() and vsnprintf() do not write more than size bytes
(including the terminating null byte (’\0’)). If the output was truncated due to
this limit then the return value is the number of characters (excluding the ter‐
minating null byte) which would have been written to the final string if enough
space had been available. Thus, a return value of size or more means that the
output was truncated. (See also below under NOTES.)

  • int fprintf(FILE *stream, const char *format, …);
    #include <stdio.h>

description:
fprintf() and vfprintf() write output to the given
output stream;
the functions fprintf() respectively, except
that they are called with a va_list instead of a variable number of arguments.
These functions do not call the va_end macro. Because they invoke the va_arg
macro, the value of ap is undefined after the call.
return value:
Upon successful return, these functions return the number of characters printed
(excluding the null byte used to end output to strings).

系统调用接口

open/creat/write/read/lseek/close

#include <fcntl.h>

  • int open(const char *pathname, int flags);
  • int open(const char *pathname, int flags, mode_t mode);
  • open不定参函数(C语言中没有函数重载)

argument:
flags:
必选:
O_RDONLY / O_WRONLY / O_RDWR
可选:
O_CREAT: 文件存在打开,不存在创建
O_EXCL: 文件不存在则创建,存在就报错返回
O_TRUNC:打开的同时清空内容
O_APPEND:追加写入

mode:以8进制指定文件权限

description:
returns a file descriptor, a small, nonnegative integer for use in subsequent sys‐tem calls.The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process;false return -1

return value:
open() and creat() return the new file descriptor, or -1 if an error occurred

库函数封装系统调用时:
r+: O_RDWR
w+: O_RDWR | O_TRUNC | O_CREAT
a+: O_RDWR | O_APPEND | O_CREAT

  • int creat(const char *pathname, mode_t mode);

argument:
mode:以8进制指定文件权限

description:
possibly create a file
creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.

return value:
open() and creat() return the new file descriptor, or -1 if an error occurred

  • ssize_t write(int fd, char* data, size_t count )

fd:文件描述符(open函数返回的句柄)
data:写入的数据首地址
count:写入的数据长度
返回值:成功返回写入的字节数,失败-1

  • ssize_t read(int fd, char* buf, size_t count)

参数:
fd: 文件描述符(open函数返回的句柄)
buf: 一块缓冲区的首地址,用于存储读取的数据
count:想要读取的数据长度
返回值:
0 实际读取的数据长度, -1错误, 0读到了文件末尾

  • off_t lseek(int fd,long offset,int whence)

argument:
whence:
SEEK_SET:The offset is set to offset bytes.
SEEK_CUR:The offset is set to its current location plus offset bytes.
SEEK_END:The offset is set to the size of the file plus offset bytes.

description:
lseek function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the directive whence as follows

return value:
fseek返回值是跳转后的位置相对于文件起始位置的偏移量

  • close(int fd);

注意事项:
1、如果open使用O_CREAT,那么就一定要加上第三个权限参数
2、权限参数是8进制,总共四位不要忘了前边这个0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值