系统:ubuntu
UNIX系统调用函数
open函数
手册页
命令:man 2 open
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);DESCRIPTION
The open() system call opens the file specified by pathname. If the
specified file does not exist, it may optionally (if O_CREAT is speci‐
fied in flags) be created by open().RETURN VALUE
open(), openat(), and creat() return the new file descriptor (a nonneg‐
ative integer), or -1 if an error occurred (in which case, errno is set
appropriately).
参数
pathname:文件地址
flags:打开权限
- 互斥:
- O_RDONLY 只读打开
- O_WRONLY 只写打开
- O_RDWR 可读可写打开
- 共享:
- O_CREAT 文件不存在则创建它。、需要第三个参数mode许可权限。
- O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。
- O_APPEND 写入时追加至文件的尾端。
- O_TRUNC 打开时将源文件内容清空。
mode:
- 属主(u,user)、属组(g,group)、其它(o,other)。
- r(read)对应数字 4、w(write)对应数字 2 、 x(execute)对应数字 1。
返回值
文件描述符,一个整数,从3开始,可以作为参数传递。打开失败返回-1 。
write函数
手册页
命令:man 2 write
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 starting at buf to the file
referred to by the file descriptor fd.RETURN VALUE
On success, the number of bytes written is returned. On error, -1 is returned, and
errno is set to indicate the cause of the error.
参数
fd:文件描述符。
buf:写入的缓存,可以是任意类型的数据。
count:写入数据的大小。
返回值
写入数据的字节数。失败返回-1。
read函数
手册页
命令:man 2 read
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.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. On error, -1 is returned, and errno is set appropriately.
参数
fd:文件描述符。
buf:读出的缓存,可以是任意类型的数据。
count:读出数据的大小。
返回值
读出数据的字节数。失败返回-1。
lseek函数
手册页
命令:man 2 lseek
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);
DESCRIPTION
lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive.RETURN VALUE
Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.
参数
fd:文件描述符。
offset:光标的相对偏移量,当前位置的偏移量为0。
whence:偏移基准位置
-
SEEK_SET:相对于文本开头的偏移。
-
SEEK_CUR:相对于当前位置的偏移。
-
SEEK_END:相对于文本末尾的偏移。
返回值
返回从文件开头到当前位置的字节偏移量。失败返回-1。
close函数
手册页
命令:man 2 close
SYNOPSIS
#include <unistd.h>int close(int fd);
DESCRIPTION
close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was asso‐ciated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).RETURN VALUE
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
参数
fd:文件描述符。
返回值
成功0,失败-1。
实例
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int openFile()
{
int fd;
fd = open("./file", O_RDWR);
if(fd == -1){
printf("file not exist!! created new!!! \n");
fd = open("./file", O_RDWR|O_CREAT, 0600);
}
return fd;
}
char* readFile(int fd)
{
char* readBuff = (char*)malloc(sizeof(char)*1024);
int size_bytes = read(fd, readBuff, 1024);
if(size_bytes == -1){
printf("Read ERROR!!!!! \n");
exit(-1);
}
return readBuff;
}
int writeFile(int fd)
{
char* writeString = "this is write data\n this is secend line\n";
int size_bytes = write(fd, writeString, strlen(writeString)); //return size or -1
if(size_bytes == -1){
printf("Write ERROR!!!!!\n");
exit(-1);
}
return size_bytes;
}
int main()
{
int fd = openFile();
int size_bytes = writeFile(fd);
printf("write size: %d\n", size_bytes);
int seeksize = lseek(fd, 0, SEEK_SET); //need move the seek to begin
char *readBuff = readFile(fd);
printf("seek: %d, readBuff: %s\n", seeksize, readBuff);
seeksize = lseek(fd, 6, SEEK_SET); //test
readBuff = readFile(fd);
printf("seek: %d, readBuff: %s\n", seeksize, readBuff);
close(fd);
return 0;
}
运行结果
scorhl@scorhl-virtual-machine:~/c/linux/file$ ./a.out
file not exist!! created new!!!
write size: 40
seek: 0, readBuff: this is write data
this is secend lineseek: 6, readBuff: s write data
this is secend line
C语言库函数
fopen函数
手册页
命令:man fopen
SYNOPSIS
#include <stdio.h>FILE *fopen(const char *pathname, const char *mode);
DESCRIPTION
The fopen() function opens the file whose name is the string pointed to by pathname and associates a stream with it.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.
参数
pathname:文件路径。
mode:权限。
-
r:只读,光标位于文本开头。
-
r+:读写,光标位于文本开头。
-
w:截断或新建只写,写入时光标位于文本开头。
-
w+:截断或新建读写,写入时光标位于文本开头。
-
a:追加或新建只写,写入时光标位于文本结尾。
-
a+:追加或新建读写,写入时光标位于文本结尾。
返回值
返回一个FILE指针,使用FILE* 接收。失败返回NULL。
fread、fwrite函数
手册页
命令:man fread、man fwrite
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 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.
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.
参数
ptr:写入的内存块地址。
size:每个元素的大小,以字节为单位。
nmemb:这是元素的个数,每个元素的大小为 size 字节。
stream:文件指针。
返回值
返回读取或写入字节大小。当size大小为1字节时,返回值等于nmemb值。
fseek函数
手册页
命令:man fseek
SYNOPSIS
#include <stdio.h>int fseek(FILE *stream, long offset, int whence);
DESCRIPTION
The fseek() function sets the file position indicator for the stream pointed to by stream.RETURN VALUE
The rewind() function returns no value. Upon 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.
参数
stream:文件指针。
offset:光标的相对偏移量,当前位置的偏移量为0。
whence:偏移基准位置
-
SEEK_SET:相对于文本开头的偏移。
-
SEEK_CUR:相对于当前位置的偏移。
-
SEEK_END:相对于文本末尾的偏移。
返回值
成功返回0。失败返回-1。
fclose函数
手册页
命令:man fclose
SYNOPSIS
#include <stdio.h>int fclose(FILE *stream);
DESCRIPTION
The fclose() function flushes the stream pointed to by stream (writing any buffered output data using fflush(3)) and closes the underly‐ing file descriptor.RETURN VALUE
Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error.
参数
stream:文件指针。
返回值
成功返回 0。失败返回 EOF。
实例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE* fopenFile(char* pathname, char* mode)
{
FILE* filePoint = fopen(pathname, mode);
if(filePoint == NULL){
printf("Open ERROR!!!!!!\n");
exit(-1);
}
return filePoint;
}
const char* freadFile(FILE* filePoint)
{
int nmemb = 1024;
char* readBuff = (char*)malloc(sizeof(char)*nmemb);
int readsize = fread(readBuff, sizeof(char), nmemb, filePoint);
return readBuff;
}
void fwriteFile(FILE* filePoint, char* data)
{
int nmemb = strlen(data);
int readsize = fwrite(data, sizeof(char), nmemb, filePoint);
if(readsize != nmemb){
printf("Write ERROR!!!!!!\n");
exit(-1);
}
printf("data write successed!!!!!!\n\n");
}
void fseekFile(FILE* filePoint, long offset, int whence)
{
int seek = fseek(filePoint, offset, whence);
if(seek == -1){
printf("Seek ERROR!!!!!!!\n");
exit(-1);
}
printf("seek moved\n\n");
}
int main()
{
char* pathname = "./ffile";
char* mode = "r";
FILE* filePoint = fopenFile(pathname, mode);
printf("filePoint: %p\n", filePoint);
const char* readBuff = freadFile(filePoint);
printf("readBuff: %s\n", readBuff);
fclose(filePoint);
mode = "w";
char* data="this is the new write\n this is the second line\n";
filePoint = fopenFile(pathname, mode);
fwriteFile(filePoint, data);
fclose(filePoint);
mode = "a+";
filePoint = fopenFile(pathname, mode);
readBuff = freadFile(filePoint);
printf("after write readBuff: %s\n", readBuff);
fseekFile(filePoint, 10, SEEK_SET);
data = "\nthis is moved seek add new line \n";
fwriteFile(filePoint, data);
fseekFile(filePoint, 0, SEEK_SET);
readBuff = freadFile(filePoint);
printf("after move seek readBuff: %s\n", readBuff);
return 0;
}
运行结果
scorhl@scorhl-virtual-machine:~/c/linux/file$ ./a.out
filePoint: 0x5e2aedf442a0
readBuff: 123456
456
789data write successed!!!!!!
after write readBuff: this is the new write
this is the second lineseek moved
data write successed!!!!!!
seek moved
after move seek readBuff: this is the new write
this is the second linethis is moved seek add new line
open与fopen
- open是UNIX系统函数,只供UNIX系统或其分支系统调用,移植性有限,返回值为整型文件描述符,默认以3开始。
- fopen是C语言库函数,在不同的系统会调用不同的系统内核API,不限系统调用,因此拥有良好的移植性,返回值为FILE指针。
- 从适用范围来看:open返回文件描述符,UNIX下的一切设备都是以文件的形式操作,如普通正规文件、网络套接字、硬件设备等。fopen是用来操纵普通正规文件的。
- 从文件IO的角度来看,open属于低级IO函数,fopen属于高级IO函数。低级文件IO运行在内核态,高级文件IO运行在用户态。
- 更多细节:https://www.cnblogs.com/NickyYe/p/5497659.html