Linux编程基础 2.1:Linux文件系统与操作

文章目录

  • 前言
  • 一、
    • 1.文件IO
      • 1.1open函数
      • 1.2 read函数
      • 1.3 write函数
      • 1.4 lseek函数
      • 1.5 close函数
    • 2 示例代码
      • 2.1 运行结果
    • 3 正确代码
      • 3.1 运行结果
  • 总结


前言

对Linux文件操作学习进行一个记录和总结


一、

1.文件IO

open()
read()
write()
lseek()
close()

1.1open函数

#include 
int open(const char *pathname, int flags[, mode_t mode);

open函数参数说明:

pathname:待打开文件的文件路径名;
flags:访问模式,常用的宏有:
– O_RDONLY:只读
– O_WRONLY: 只写
– O_RDWR: 读写
– O_CREAT: 创建一个文件并打开
– O_EXCL: 测试文件是否存在,不存在则创建
– O_TRUNC: 以只写或读写方式成功打开文件时,将文件长度截断为0
– O_APPEND: 以追加方式打开文件
只有第二个参数flags = O_CREAT,第三个参数才会被用于设置新文件的权限,取值如下:

S_IRWXU: 文件所有者,读、写、执行
S_IRUSR: 文件所有者,读
S_IWUSR: 文件所有者,写
S_IXUSR: 文件所有者,执行
S_IRWXG: 文件所属组,读、写、执行
S_IRGRP: 文件所属组,读
S_IWGRP: 文件所属组,写
S_IXGRP: 文件所属组,执行
S_IRWXO: 其他人,读、写、执行
S_IROTH: 其他人,读
S_IWOTH: 其他人,写
S_IXOTH: 其他人,执行

返回值说明:

调用成功,返回一个文件描述符
不成功,返回-1
例: 创建一个文件

open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
or
int create(const char *pathname, mode_t mode);

1.2 read函数

#include 
ssize_t read(int fd, void *buf, size_t count);

read函数参数说明:

fd: 从open或create函数返回的文件描述符
buf: 缓冲区
count: 读取数据的字节数
返回值说明:

ssize_t: 有符号的size_t,有三种返回值
– 正数:请求读取的字节数
– 0: 文件长度有限,若读写位置距文件末尾只有20字节,该函数请求读取30字节,则第一次读取时返回值为20,第二次读取时,返回0
– -1: 读取文件出错
特殊说明: read函数从设备或网络中读数据,如从终端读取数据,终端写入数据没回车,这些数据不会传给read函数,read函数就会一直阻塞;如从网络端读取数据,网络通信的socket文件没有数据,read函数同样会阻塞。

1.3 write函数

#include 
ssize_t write(int fd, void *buf, size_t count);

write函数参数说明: 同read函数
返回值说明: 返回写入的字节数或者-1并设置errno
特殊说明: 向终端或网络端写数据时,可能会进入阻塞状态

1.4 lseek函数

#include 
ssize_t lseek(int fd, off_t offset, int whence);

lseek函数参数说明:

fd: 从open或create函数返回的文件描述符
offset: 对文件偏移量的设置,参数可正可负
whence: 控制设置当前文件偏移量的方法
– whence = SEEK_SET: 文件偏移量被设置为offset
– whence = SEEK_CUR: 文件偏移量被设置为当前偏移量+offset
– whence = SEEK_END: 文件偏移量被设置为文件长度+offset
返回值说明:

设置成功:返回新的偏移量
不成功:-1

1.5 close函数

#include 
int close(int fd);

返回值说明:

成功:返回0
不成功:-1

2 示例代码

#include 
#include 
#include 
#include 
#include 
int main(){
	int tempFd = 0;
	char tempFileName[20] = "test.txt";
	//Step 1. open the file.
	tempFd = open(tempFileName, O_RDWR|O_EXCL|O_TRUNC, S_IRWXG);
	if(tempFd == -1){
		perror("file open error.\n");
		exit(-1);
	}//of if
	//Step 2. write the data.
	int tempLen = 0;
	char tempBuf[100] = {0}
	scanf("%s", tempBuf);
	tempLen = strlen(tempBuf);
	write(tempFd, tempBuf, tempLen);
	close(tempFd);
	//Step 3. read the file
	tempFd = open(tempFileName, O_RDONLY);
	if(tempFd == -1){
		perror("file open error.\n");
		exit(-1);
	}//of if
	off_t tempFileSize = 0;
	tempFileSize = lseek(tempFd, 0, SEEK_END);
	lseek(tempFd, 0, SEEK_SET);
	while(lseek(tempFd, 0, SEEK_CUT)!= tempFileSize){
		read(tempFd, tempBuf, 1024);
		printf("%s\n", tempBuf);
	}//of while
	close(tempFd);
	return 0;
}//of main

2.1 运行结果

Linux编程基础 2.1:Linux文件系统与操作_第1张图片第一次运行报错,查明原因后发现是代码中的test.txt文件没有在代码运行前创建好
Linux编程基础 2.1:Linux文件系统与操作_第2张图片
第二次运行报错竟然是忘记加";",大家在写代码时一定尽量避免这个小错误
Linux编程基础 2.1:Linux文件系统与操作_第3张图片
第三次运行报错是参数常量写错了,应该是SEEK_CUR

3 正确代码

#include 
#include 
#include 
#include 
#include 
int main(){
	int tempFd = 0;
	char tempFileName[20] = "test.txt";
	//Step 1. open the file.
	tempFd = open(tempFileName, O_RDWR|O_EXCL|O_TRUNC, S_IRWXG);
	if(tempFd == -1){
		perror("file open error.\n");
		exit(-1);
	}//of if
	//Step 2. write the data.
	int tempLen = 0;
	char tempBuf[100] = {0};
	scanf("%s", tempBuf);
	tempLen = strlen(tempBuf);
	write(tempFd, tempBuf, tempLen);
	close(tempFd);
	//Step 3. read the file
	tempFd = open(tempFileName, O_RDONLY);
	if(tempFd == -1){
		perror("file open error.\n");
		exit(-1);
	}//of if
	off_t tempFileSize = 0;
	tempFileSize = lseek(tempFd, 0, SEEK_END);
	lseek(tempFd, 0, SEEK_SET);
	while(lseek(tempFd, 0, SEEK_CUR)!= tempFileSize){
		read(tempFd, tempBuf, 1024);
		printf("%s\n", tempBuf);
	}//of while
	close(tempFd);
	return 0;
}//of main

3.1 运行结果

在这里插入图片描述
运行成功

总结

  1. 学习了Linux文件操作相关函数,理解了其中相关参数和返回值的含义
  2. 学习了c语言编写时的代码规范,例如临时变量命名为tempXXX,函数结构、循环结构末尾注释 of xxx

参考:Linux编程基础 2.1:Linux文件系统与操作_HenrySmale的博客-CSDN博客

你可能感兴趣的:(linux,网络,运维,c语言)