Linux 高并发学习笔记 - Linux 文件操作函数

1.6.2 Linux 文件操作函数

Linux 高并发学习笔记 - 笔记索引

前言

关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown用起来太繁琐了。那么关于函数这一块主要也是读文档就能解决的了。

关于文档可以使用Linux命令man 2 xxx查看,在后面也写了查看每个文档的命令。

还是需要再次声明的,Linux的文件读写效率低于标准C库的文件读写效率,而本文主要介绍Linux文件读写函数,毕竟这是Linux编程的基础篇嘛。在一般应用场合使用标准C库文件读写函数更好,特别场合使用Linux文件读写函数,至于为什么,凉皮在上篇文章中写到过。

打开文件
  • open
  • O_RDONLYO_WRONLYO_RDWRO_APPENDO_NONBLOCKO_CREAT
  • UMASK:在后文的权限操作中,也会与UMASK操作后处理。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// 	open a file
// 		pathname:
// 			the file path to open
// 		flags:
// 			must and exactly one:
// 				O_RDONLY 	for read only, 
// 				O_WRONLY 	for write only, 
// 				O_RDWR 		for read and write
// 			optional and limitless:
// 				O_APPEND 	for append mode, 
// 				O_NONBLOCK 	for non-block, 
// 				O_CREAT 	for creatable.
// 		mode:
// 			If O_CREATE flag is on, must follow with the key.
// 			Mode is a oct-based number and determine the limits of the new file.
// 			It includes three digits (such as 0664), for user - group - others.
//			And each digit can be extend to three binary-bits, for read - write - execute.
// 			The finally mode will be (mode^~UMASK).
// 			* UMASK:
// 				A mask on the mode to ensure the file didn't give the inappropriate permission. 
// 				Also you can change the value of UMASK, command "$ umask" to show the value.
// 				The default value is 0002. Because giving others write permission is inappropriate.
// 		return value:
// 			file descripor, or -1 for error
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("open");  // About more: $ man 3 perror

// About more
// $ man 2 open
关闭文件
  • close
#include <unistd.h>
// 	close a file
// 	file will automatic close while process ended
// 		fd:
// 			file description
// 		return value:
// 			return 0 for success, -1 for error
int close(int fd);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("close");

// About more
// $ man 2 close
读取文件
  • read
#include <unistd.h>
// 	read a file
// 		fd:
// 			file descriptor of a readable file
// 		buf:
// 			he container of data
// 		count:
// 			read size for each time
// 		return value:	
// 			real read size of data, or -1 for error
// 			Because the rest size maybe smaller than ideal. It become zero if read the end of file.
ssize_t read(int fd, void *buf, size_t count);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("read");

// About more
// $ man 2 read
写入文件
  • write
#include <unistd.h>
// similar to read a file
ssize_t write(int fd, const void *buf, size_t count);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("write");

// About more
// $ man 2 write
文件指针
  • lseek
  • SEEK_SETSEEK_CURSEEK_END
#include <sys/types.h>
#include <unistd.h>
// 	reposition read / write file offset
// 		fd:
// 			file descriptor
// 		offset:
// 			relative offset to whence
// 		whence:
// 			SEEK_SET for BOF, SEEK_CUR for now file offset, SEEK_END for EOF
// 		return value:
// 			absolute file offset
off_t lseek(int fd, off_t offset, int whence);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("lseek");

// About more
// $ man 2 lseek
int offset = lseek(fd, 0, SEEK_CUR);	// get now file offset
lseek(fd, offset, SEEK_SET);			// reposition to absolute offset
lseek(fd, 0, SEEK_END);					// reposition to the end of file
文件截断
  • truncate
#include <unistd.h>
#include <sys/types.h>
// 	resize the file
// 		path:
// 			the file path
// 		length:
// 			new length of file
// 			append \x00 if become larger and cut down the rest tail if become smaller
// 		return value:
// 			return 0 for success, -1 for error
int truncate(const char *path, off_t length);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("truncate");

// About more
// $ man 2 truncate
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值