此系列笔记参考华清远见《嵌入式 LINUX 应用程序开发标准教程》
首先介绍几个基本的文件IO操作函数:
- open()函数用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数。
- close()函数用于关闭一个被打开的文件。当一个进程终止时,所有被它打开的文件都由内核自动关闭,很多程序都使用这一功能而不显示地关闭一个文件。
- read()函数用于将从指定的文件描述符中读出的数据放到缓存区中,并返回实际读入的字节数。若返回 0,则表示没有数据可读,即已达到文件尾。读操作从文件的当前指针位置开始。当从终端设备文件中读出数据时,通常一次最多读一行。
- write()函数用于向打开的文件写数据,写操作从文件的当前指针位置开始。对磁盘文件进行写操作,若磁盘已满或超出该文件的长度,则 write()函数返回失败。
- lseek()函数用于在指定的文件描述符中将文件指针定位到相应的位置。它只能用在可定位(可随机访问)文件操作中。 管道、 套接字和大部分字符设备文件是不可定位的, 所以在这些文件的操作中无法使用 lseek()调用。
下面是代码copy_file.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 1024 /* 每次读写缓存大小,影响运行效率*/
#define SRC_FILE_NAME "src_file" /* 源文件名 */
#define DEST_FILE_NAME "dest_file" /*目标文件名*/
#define OFFSET 10240 /*复制的数据大小*/
int main()
{
int src_file,dest_file;
unsigned char buff[BUFFER_SIZE];
int real_read_len;
//以只读的方式打开源文件
src_file = open(SRC_FILE_NAME,O_RDONLY);
//以只写的方式打开目标文件,若文件不存在则创建该文件,访问权限值为644
dest_file = open(DEST_FILE_NAME,
O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(src_file <0 || dest_file <0)
{
printf("Open file error \n");
exit(1);
}
//将源文件的读写指针移动到最后10kB的起始位置
lseek(src_file,-OFFSET,SEEK_END);
//读取源文件的最后10kB数据并写入到目标文件中,每次都写1kB
while((real_read_len = read(src_file,buff,sizeof(buff)))>0)
{
write(dest_file,buff,real_read_len);
}
close(dest_file);
close(src_file);
return 0;
}
需要注意的是实验的步骤:
- 首先新建src_file(注意没有后缀扩展名)
vim src_file
然后在里面写至少1024字符,如下所示:
ddsfafffffsdfjafhaslhfa hfaskjga sg as ghjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgjadsfafffffsdfjafhaslhfa hfaskjga sg ashjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgjadsfafffffsdfjafhaslhfa hfaskjga sg as ghjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgjadsfafffffsdfjafhaslhfa hfaskjga sg as ghjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgjadsfafffffsdfjafhaslhfa hfaskjga sg as ghjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgjasfafffffsdfjafhaslhfa hfaskjga sg as ghjkadshgjkdhajkghadjsghajkdbndsbjkfdsagkjhadskjghajkghaghalhgalhgjahgjkahgjlahgkjahjgahjkghajghajkghajkhgjkdahgkjahjgkahjgahjghajdghasjkghsjahgdjakhgjasdhgjkdsahgjkdsahvkdsnvdbhgdhgdhajkgdhsajghdajghdsajhdjakfhajkdhfjkdashfjkahfieulwhgsdklhgjdashgjahgja
- 编译一下
gcc -o copy_file copy_file.c
- 运行
./copy_file
这时会在当前文件夹下看到一个dest_file,打开看下会发现和src_file前1024字符是一样的。