-
文件的操作
1.1 文件描述符:
1) 用来描述所要操作的文件的符号,在Linux系统中采用的是一个整型数据来描述。
2) 它是一个非负整数;
3) 由系统分配:
在打开或者创建文件的时候进行分配。
顺序分配,具体指将未被使用的最小的哪一个非负整数分配出来。
4) 在默认情况下,一个进程运行起来会默认分配三个特殊的文件描述符;
0 : 标准输入 scanf
1 : 标准输出 printf
2 :标准错误输出 perror
5) 用来操作对应的文件1.2 文件的具体操作及对应API接口
1) 打开文件/创建文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* 老式版本 */
int open(const char *pathname, int flags);
int creat(const char pathname, mode_t mode);
/ 新式 */
int open(const char *pathname, int flags, mode_t mode);
参数:
参数1:pathname表示所要打开或者创建文件的路径(相对路径/绝对路径);
参数2:flags打开文件的方式:
注意:以下三个必须包含其中的一个:
O_RDONLY: 只读方式打开
O_WRONLY: 只写方式打开
O_RDWR: 可读写
O_CREAT : 创建文件,此时参数3生效;没有设置的时候,表示打开文件
如果文件不存在则创建,文件存在则打开,保留原有的数据;
O_EXCL :创建文件,如果文件不存在则创建,文件存在则报错;
O_TRUNC : 创建文件,如果文件不存在则创建,文件存在则打开(清除原有的数据)
O_APPEND: 创建文件,如果文件不存在则创建,文件存在则打开(保留原文件的数据,并且新写的内容会追加的文件的末尾)参数3:mode表示所创建文件的权限。 mode&~mask 返回值:成功返回文件描述符,失败返回-1,且修改errno的值(可以是用perror函数去查看具体的错误信息)。 2) 读文件 #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); 参数: 参数1:fd是所要读文件的文件描述符,表示从哪一个文件中去读取数据 参数2:buf是缓存空间的起始地址,表示读取到的数据存储到那里 参数3: count是一个大小;表示的是期望读取数据的大小。 if (count == 0) 返回值为0或者其它值。 if (count > SSIZE_MAX) count = SSIZE_MAX 返回值: 成功返回 1) 读取数据的字节数; 2) 0,读取到文件的末尾; 失败返回-1,且修改errno的值 3) 写文件 #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); 参数: 参数1:fd是所要写文件的文件描述符,表示把数据写入到那里去 参数2:buf是缓存空间的起始地址,表示所要写数据存储的位置 参数3: count是一个大小;表示的是期望写入数据的大小。 返回值: 成功返回0 失败返回-1.且修改errno的值 4) 文件的定位 图片的马赛克处理: #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence); 参数: 参数1:fd是所要操作文件的文件描述符 参数2:offset表示的是偏移量,<0 向文件的开始偏移,>0 向文件结束方向偏移; 参数3: whence表示的是基准, SEEK_SET : 文件的起始位置 SEEK_CUR : 文件当前位置 SEEK_END : 文件结束位置 5) 关闭文件 #include <unistd.h> int close(int fd); 参数fd,表示的是所要关闭的文件的文件描述符; 返回值:成功返回0,失败返回-1,且修改
3.基本小程序【open close read write lseek】
Part 1:close
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
int main()
{
close(1);
printf("hello\n");
return 0;
}
Part 2:open
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
#if 0
/* 打开已经存在的文件1.txt,如果文件不存在则报错,存在则成功打开 */
fd = open("1.txt", O_RDWR);
if (-1 == fd) {
perror("open");
return -1;
}
#endif
#if 1
/* 打开文件1.txt,如果文件存在则直接打开,不存在则创建文件 */
fd = open("1.txt", O_RDWR | O_CREAT, 0777);
if (-1 == fd) {
perror("open");
return -1;
}
#endif
/* 打开文件1.txt,如果文件存在则直接打开,不存在则创建文件 */
fd = open("1.txt", O_RDWR | O_CREAT | O_TRUNC, 0777);
if (-1 == fd) {
perror("open");
return -1;
}
#if 0
/* 创建文件1.txt,如果文件不存在则创建文件,如果文件存在则报错 */
fd = open("1.txt", O_RDWR | O_CREAT | O_EXCL, 0777);
if (-1 == fd) {
perror("open");
return -1;
}
#endif
printf("fd = %d\n", fd);
}
Part 3:read
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd;
int ret;
char buf[256];
/* 以只读的方式打开文件1.txt */
fd = open("1.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return -1;
}
while(1) {
/* 将临时空间数据清零 */
memset(buf, 0, sizeof(buf));
/* 读取文件中的数据存储到临时空间buf中 */
ret = read(fd, buf, sizeof(buf));
if (ret == -1) { /* 读取文件失败 */
perror("read");
return -1;
} else if (ret == 0) { /* 读取到文件的末尾 */
break;
}
/* 将读取的数据通过标准输出 */
printf("%s", buf);
}
return 0;
}
Part 4:write
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd;
int ret;
char buf[256];
/* 以只写的方式打开文件2.txt */
fd = open("2.txt", O_WRONLY | O_CREAT | O_APPEND, 0777);
if (fd == -1) {
perror("open");
return -1;
}
/* 向文件fd中写数据 */
scanf("%s", buf);
ret = write(fd, buf, strlen(buf));
if (ret == -1) {
perror("write");
return -1;
}
return 0;
}
Part 5:lseek指针定位
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd;
int ret;
char buf[256];
/* 以只读的方式打开文件1.txt */
fd = open("1.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return -1;
}
/* 从当前位置向后偏移10个字节 */
if ((off_t)-1 == lseek(fd, 10, SEEK_CUR)) {
perror("lseek");
return -1;
}
while(1) {
/* 将临时空间数据清零 */
memset(buf, 0, sizeof(buf));
/* 读取文件中的数据存储到临时空间buf中 */
ret = read(fd, buf, sizeof(buf));
if (ret == -1) { /* 读取文件失败 */
perror("read");
return -1;
} else if (ret == 0) { /* 读取到文件的末尾 */
break;
}
/* 将读取的数据通过标准输出 */
printf("%s", buf);
}
return 0;
}
最后:专题一【文件拷贝】
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
int file=open("wuhan.txt",O_RDONLY);//打开已经创建的文件wuhan.txt
if(file==-1){
perror("open");
exit(1);}//打开失败
int newfile=open("chengdu.txt",O_CREAT|O_WRONLY,0777);
if(newfile==-1){
perror("open");
exit(1);}
while(1){
int buff[1000]={0};
int count=read(file,buff,999);
if(count==-1){
perror("read");
exit(1);
}
else if(count==0){
break;
}
write(newfile,buff,count); }
close(file);
close(newfile);
}