Linux 文件I/O

基本介绍

0.基本概述

  1. 文件编程(如何用代码操作文件,实现文件创建,打开,编辑等自动化执行)
  2. 所有执行 I/O 操作的系统调用都以文件描述符,一个非负整数(通常是小整数),来指代打开的文件。文件描述符用以表示所有类型的已打开文件,包括管道(pipe)、FIFO、socket、终端、设备和普通文件。针对每个进程,文件描述符都自成一套。

Linux 有很多API 用于对文件操作、如下:

1.打开文件

头文件

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

函数原型

int open(const char *pathname, int flags);				//当文件存在可以用它
int open(const char *pathname, int flags, mode_t mode);	//当文件存在或者不存在都可以用。

参数

  • pathname:

    • 文件名
  • flags:

    • 以什么方式打开

    • O_RDONLY : 只读方式

    • O_WRONLY : 只写方式

    • O_RDWR : 可读可写方式

    • 以上这三个常数中应当只指定一个,下列常数是可选择的。

    • O_CREAT : 若文件不存在则创建它,使用此选项时,需要同时说明第三个参数mode 用其说明该新文件的存取许可权限。

    • O_EXCL : 如果同时指定了OCREAT 而文件已经存在,则出错,打开文件失败(返回值为-1)。

    • O_APPEND : 每次写时都加到文件的尾端。

    • O_TRUNC : 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或者只写成功打开,则将其长度截短为0;

  • mode:

    • 一定是在flags中使用了O_CREAT这个标志,mode记录创建的文件的访问权限。
      • S_IRWXU 00700 权限,代表该文件所有者具有可读、可写及可执行的权限。
      • S_IRUSR 00400 权限,代表该文件所有者具有可读取的权限。
      • S_IWUSR 00200 权限,代表该文件所有者具有可写入的权限。
      • S_IXUSR 00100 权限,代表该文件所有者具有可执行的权限。
      • S_IRWXG 00070 权限,代表该文件用户组具有可读、可写及可执行的权限。
      • S_IRGRP 00040 权限,代表该文件用户组具有可读的权限。
      • S_IWGRP 00020 权限,代表该文件用户组具有可写入的权限。
      • S_IXGRP 00010 权限,代表该文件用户组具有可执行的权限。
      • S_IRWXO 00007 权限,代表其他用户具有可读、可写及可执行的权限。
      • S_IROTH 00004 权限,代表其他用户具有可读的权限
      • S_IWOTH 00002 权限,代表其他用户具有可写入的权限。
      • S_IXOTH 00001 权限,代表其他用户具有可执行的权限。

返回值

  • 失败: -1
  • 成功: 文件描述符

打开存在的文件示例

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

#define		FILENAME	  "xx"		//要打开的文件名

int main(int argc,char **argv)
{
	int fd = open(FILENAME,O_RDWR);	//可读可写方式打开文件
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}

	close(fd);	//关闭文件
	return 0;
}

打开文件如果不存在则创建示例

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

#define         FILENAME        "xx"            //要打开的文件名

int main(int argc,char **argv)
{
	int fd = open(FILENAME,O_RDWR | O_CREAT,00700); //如果文件不存在则创建文件 权限:可读可写
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}
	close(fd);      //关闭文件
	return 0;
}

2.写文件

头文件

#include <unistd.h>

函数原型

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

参数

  • fd:
    • 文件描述符
  • buf:
    • 写入的内容
  • count:
    • 写入的字节数

返回值

  • 失败: -1
  • 成功: 返回写入的字节数

对文件写入的示例 (写入 hello word)

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

#define		FILENAME	"xx"		//要打开的文件名

int main(int argc,char **argv)
{
	int fd = open(FILENAME,O_RDWR | O_CREAT,00700);	//如果文件不存在则创建文件 权限:可读可写
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}
	
	char buf[] = "hello world";
	int w_len = write(fd,buf,strlen(buf));
	if(-1 == w_len)
	{
		perror("write");
		return -1;
	}
	else
	{
		printf("writing %d byte successed\n",w_len);
	}
	
	
	close(fd);	//关闭文件
	return 0;
}

3.读文件

头文件

#include <unistd.h>

函数原型

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

参数

  • fd:
    • 文件描述符
  • buf:
    • 存放读取的内容
  • count:
    • 读取的字节数

返回值

  • 失败: -1
  • 成功: 返回读取到的字节数 如果是 0 则是读到文件结束。

读取文件内容示例

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

#define		FILENAME	"xx"		//要打开的文件名

int main(int argc,char **argv)
{
	int fd = open(FILENAME,O_RDWR | O_CREAT,00700);	//如果文件不存在则创建文件 权限:可读可写
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}
	
	char buf[12] = {0};
	while(1)
	{
		int r_len = read(fd,buf,sizeof(buf)-1);
		if(-1 == r_len)
		{
			perror("write");
			return -1;
		}
		else if(0 == r_len)
		{
			printf("read done!\n");
			break;
		}
		else
		{
			printf("%s",buf);
		}
		memset(buf,0,sizeof(buf));
	}
	
	close(fd);	//关闭文件
	return 0;
}

4.改变文件偏移量

头文件

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

函数原型

off_t lseek(int fd, off_t offset, int whence);

参数

  • fd:
    • 文件描述符
  • offset:
    • 有符号整数(基于参数 whence 位置的偏移量)
  • whence:
    • SEEK_SET
      将文件偏移量设置为从文件头部起始点开始的 offset 个字节。
    • SEEK_CUR
      相对于当前文件偏移量,将文件偏移量调整 offset 个字节 。
    • SEEK_END
      将文件偏移量设置为起始于文件尾部的 offset 个字节。也就是说,offset 参数应该从文件最后一个字节之后的下一个字节算起。

whence参数图解
在这里插入图片描述
返回值

  • 失败: -1
  • 成功: 返回目前的读写位置, 也就是距离文件开头多少个字节

ps:

  • 用lseek() 可以测出文件内容多大。

打开文件写入并读取内容示例

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

#define		FILENAME	"xx"		//要打开的文件名

int main(int argc,char **argv)
{
	int fd = open(FILENAME,O_RDWR | O_CREAT,00700);	//如果文件不存在则创建文件 权限:可读可写
	if(-1 == fd)
	{
		perror("open");
		return -1;
	}

	char sendbuf[12] = "hello world";
	char recvbuf[12] = {0};
	
	//把内容写入文件
	int n_w = write(fd,sendbuf,strlen(sendbuf));
	if(-1 == n_w)
	{
		perror("write");
		return -1;
	}
	
	//移动光标回到文件头
	int ret = lseek(fd,0,SEEK_SET);
	if(-1 == ret)
	{
		perror("lseek");
		return -1;
	}
	
	//读取文件内容	
	while(1)
	{
		ret = read(fd,recvbuf,sizeof(recvbuf)-1);
		if(-1 == ret)
		{
			perror("read");
			return -1;
		}
		else if(0 == ret)
		{
			printf("读取完毕\n");
			break;
		}

		printf("%s",recvbuf);
		memset(recvbuf,0,sizeof(recvbuf));
	}
	close(fd);	//关闭文件
	return 0;
}

5.关闭文件

头文件

#include <unistd.h>

函数原型


int close(int fd);

参数
fd:

  • 文件描述符

返回值

  • 成功: 0
  • 失败: -1

示例

close(fd);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮卡丘吉尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值