LINUX系统编程(文件的操作)
LINUX系统编程(文件的操作)
LINUX操作系统为文件的操作提供了一系列的API,如:打开open,读写write/read,光标定位lseek,关闭close。
1.打开/创建文件
函数的原型,包含的头文件,返回值
NAME
open, creat - open and possibly create a file or device
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);
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建文件的访问权限
int creat(const char *pathname, mode_t mode);
DESCRIPTION
Given a pathname for a file, open() returns a file descriptor, a small,
nonnegative integer for use in subsequent system calls (read(2),
write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a
successful call will be the lowest-numbered file descriptor not cur‐
rently open for the process.
Pathname:要打开的文件名(含路径,缺省为当前路径)
Flags: O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR可读可写打开
当我们附带这些权限后,打开的文件就只能按上三种权限来操作。下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用它时需要说明第三个参数mode,说明该新文件的存取许可权限。
O_EXCL 如果同时指定了OCREAT,而文件已经存在,则打开失败或者返回值是-1.
O_APPEND 每次写时都加到文件末端。
O_TRUNC属性去打开文件时,如果这个文件本身有内容,而且为只读或只写成功打开,则其长度截断为0
open函数返回一个文件描述符fd(小的非负整数)(打开失败返回-1),对后面一系列读写光标等都需要fd,涉及到Linux内核对文件管理的操作。
2.写文件
函数的原型,包含的头文件,返回值
NAME
write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
第一个参数:文件描述符
第二个参数:无类型指针,是一个缓冲区
第三个参数:要写入文件的大小,字符串可用strlen
DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file referred to by the
file descriptor fd.
描述:将缓冲区buf这个指针指向内存的位置里面的数据写这么多个字节,写到fd对应的文件
RETURN VALUE
On success, the number of bytes written is returned (zero indicates nothing was written).
On error, -1 is returned, and errno is set appropriately.
成功的话返回的是一个整型数,返回的是写入的个数,失败返回-1
写完记得close
3.读文件
函数的原型,包含的头文件,返回值
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.
描述:从fd指向的文件读取count个字节的数据,放到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 返回值是数,成功的话读入多少返回多少,失败返回-1
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.
4.光标移动
函数的原型,包含的头文件,返回值
NAME
lseek - reposition read/write file offset
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
第一个参数:文件描述符
第二个参数:对whence的偏移值
第三个参数:位置
DESCRIPTION
The 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:
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.
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.
5.小例子(创建,写,读)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int fd;
char *buf = "lpl";
char *readbuf;
fd = open("./file1",O_RDWR|O_CREAT,0600);
int n_write = write(fd,buf,strlen(buf));
lseek(fd,0,SEEK_SET);
readbuf = (char *)malloc(sizeof(char)*n_write + 1);
int n_read = read(fd,readbuf,n_write);
printf("contexe = %s\n",readbuf);
close(fd);
return 0;
}
6.其他权限的补充
O_EXCL 如果同时指定了OCREAT,而文件已经存在,则打开失败或者返回值是-1.
O_APPEND 每次写时都加到文件末端。
O_TRUNC属性去打开文件时,如果这个文件本身有内容,而且为只读或只写成功打开,则其长度截断为0
代码略
创建文件Creat函数
函数的原型,包含的头文件,返回值
int creat(const char *pathname, mode_t mode);
pathname:要创建的文件名(含路径)
mode:创建模式(可读可写可执行)
常见的创建模式:
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读可写可执行
int main()
{
int fd;
fd = creat("./test",S_IRWXU);
return 0;
}
7.标准C库打开创建文件,读写文件,光标移动
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
路径 权限
size_t fwrite(const void *ptr, size_t size , size_t nmemb ,FILE *stream);
缓冲区(buf) 一个字符的大小 字符的个数 哪个文件
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
缓冲区(buf) 一个字符的大小 字符的个数 哪个文件
int fseek(FILE *stream, long offset, int whence);和lseek 一样
**fwrite和fread的返回值取决于第三个参数。**
例子:
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char *str = "lpl";
char readbuf[128]={0};
fp = fopen("./file1.text","w+");
fwrite(str,sizeof(char),strlen(str),fp);
// fwrite(str,sizeof(char)*strlen(str),1,fp);
fseek(fp,0,SEEK_SET);
fread(readbuf,sizeof(char),strlen(str),fp);
// fread(readbuf,sizeof(char)*strlen(str),1,fp);
printf("%s\n",readbuf);
return 0;
}
8.标准C库其他文件函数
SYNOPSIS
#include <stdio.h>
int fputc(int c, FILE *stream);把一个整数或字符写入文件
int fgetc(FILE *stream);一个个字符从文件取出来
int feof(FILE *stream);是否到达文件尾部,到尾部返回0,没有到尾部返回非0
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
int i;
char *str = "welcome to beijing!";
int len = strlen(str);
fp = fopen("./file.text","w+");
for(i=0;i<len;i++){
fputc(*str,fp);
str++;
}
fclose(fp);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
int i;
char c;
fp = fopen("./file.text","r+");
while(!feof(fd)){
c = fgetc(fp);
printf("%c",c);
}
fclose(fp);
return 0;
}
小结
写到的都是基础,略微简单。

版权声明:本文为qq_53022114原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。