【Linux】文件操作函数(系统调用函数)

  • 重点在于学习——思路与方法
  • 举一反三

一、文件描述符

  • 系统分配给文件的数字编号

二、函数学习

P.S.Man命令使用方法

	manual 前三个章节 命令;系统调用函数;库函数
	man read //read命令
	man 2 read //系统调用函数read

第2类 系统调用文件编程类

2.1 打开文件

2.1.1 函数名
open
2.1.2 函数原形
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
2.1.3 函数功能
open and possibly create a file or device
2.1.4 所属头文件
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
2.1.5 返回值
return the new file descriptor, 
or -1 if an error occurred .
2.1.6 参数说明
pathname	//文件路径名
flags		//access mode:O_RDONLY,O_WRONLY,or O_RDWR.more flags to search——man COMMAND	
-O_APPEND:以追加方式打开文件
-O_CREAT:当打开的文件不存在的时候,创建该文件
mode: // if flag = O_CREAT , mode 用于设置新创建文件的访问权限

P.S.参数可位或
fd = open ("/home/test.c".O_RDWR|O_CREAT,0755);

2.2 创建文件

2.2.1 函数名
creat
2.2.2 函数原形
int creat(const char *pathname, mode_t mode);
2.2.3 函数功能
creat() is equivalent to open()  with  flags  equal  to O_CREAT|O_WRONLY|O_TRUNC.
创建一个文件,并以只写的方式打开该文件
2.2.4 所属头文件
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
2.2.5 返回值
success:file descriptior
fali:	 -1
2.2.6 参数说明
pathname	//文件路径名
mode		//新建文件的读写权限设置

2.3 关闭文件

2.3.1 函数名
close
2.3.2 函数原形
 int close(int fd);
2.3.3 函数功能
关闭一个打开的文件
2.3.4 所属头文件
#include <unistd.h>
2.3.5 返回值
success:0
error  :-1
2.3.6 参数说明
fd :待关闭文件的file descriptor

2.4 读文件

2.4.1 函数名
read
2.4.2 函数原形
//-man 2 read
ssize_t read(int fd, void *buf, size_t count);
2.4.3 函数功能
从一个打开的文件中读取数据
2.4.4 所属头文件
#include <unistd.h>
2.4.5 返回值
success:返回读取的字节数
error:-1
2.4.6 参数说明
fd :待读取数据的文件的file descriptor
count:希望读取的字节数
buf:  读取来的数据存到buf指向的缓冲区

2.5 写文件

2.5.1 函数名
write
2.5.2 函数原形
//-man 2 read
ssize_t write(int fd, const void *buf, size_t count);
2.5.3 函数功能
向一个打开的文件写入数据
2.5.4 所属头文件
#include <unistd.h>
2.5.5 返回值
success:写入到文件里的字节数
error:-1
2.5.6 参数说明
fd :待写入数据的文件的file descriptor
count:希望写入的字节数
buf:  要写入数据的存放位置

2.6 重定位文件读写位置

2.6.1 函数名
lseek
2.6.2 函数原形
  off_t lseek(int fd, off_t offset, int whence);
2.6.3 函数功能
reposition read/write file offset
重新定位文件的读写位置(指针)
2.6.4 所属头文件
  #include <sys/types.h>
  #include <unistd.h>
2.6.5 返回值
  success:返回移动后的文件指针距离文件开头的字节数位置
  error:-1
2.6.6 参数说明
  fd :待写入数据的文件的file descriptor


  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:
  //无论指针现在在何处,whence设置offset开始移动的起始位置
   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.

2.7 复制文件描述符

2.7.1 函数名
dup
2.7.2 函数原形
int dup(int oldfd)
2.7.3 函数功能
复制一个文件描述符
2.7.4 所属头文件
#include <unistd.h>
2.7.5 返回值
success:返回新的文件描述符
error: -1
2.7.6 参数说明
oldfd:待复制的旧的文件描述符

三、综合实例 ——手动实现cp命令功能

#include <sys/types.h>
#include <sys/stat.h> 
#include <fcntl.h>
#include <unistd.h>

void main(int argc,char **argv)
{
	int fd_source;
	int fd_destination;
	char buf[512];
	int count = 0;
/*1.打开源文件*/
	fd_source = open(argv[1],O_RDONLY);

/*2.打开目标文件*/
	fd_destination = open(argv[2],O_RDWR|O_CREAT,0666);

/*3.读取源文件,循环分块读写*/
	while(count = read (fd_source,buf,512)>0)
/*4.将数据写入目标文件*/
	{
		write(fd_destination,buf,count);
	}
/*5.关闭文件*/
	close(fd_source);
	close(fd_destination);
 }

原文地址:https://www.cnblogs.com/Neo007/p/7281069.html