文件I/O接口函数详解

标准IO和文件IO的区别

标准IO

  • 符合ANSI C标准。
  • 带有缓冲区,减少系统调用,提高系统效率。
  • 通过流(FILE结构体)来表示一个打开的文件。
  • 只能访问普通文件和终端文件。

文件IO

  • 符合POSIX(可移植操作系统接口)标准。
  • 不具有缓冲机制。
  • 采用文件描述符fd来表示一个打开的文件。
  • 可以访问各种类型文件

标准IO是基于文件IO实现的。在文件IO增加了缓冲机制。

文件描述符

文件描述符是一个非负数。Linux系统为每个打开的文件分配文件描述符。在一个程序中,从0开始依此递增。文件IO操作通过文件描述符完成。
由于 标准IO是基于文件IO实现的。所以在标准IO中的标准输入/输出/错误流对应了文件IO中的文件描述符。
标准输入stdin,标准输出stdout,标准错误stderr,对应的文件描述符为 0,1,2。

接口函数

open

头文件和函数接口
 #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);

flags意义
pathname文件路径
O_RDONLY只读方式打开
O_WRONLY可写方式打开
O_RDWR读写方式打开
O_CREAT文件不存在则创建文件,用flags指定存取文件
O_EXCL用O_CREAT创建文件时返回错误信息以测试文件是否存在
O_NOCTTY文件为终端时,不能为调用open()系统调用的那个进程
O_TRUNC若文件存在则删除文件中原有数据
O_APPEND以添加数据在原文件末尾的方式打开
测试代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main()
{
	int fd;
	char *test1="/bin/test1";
	char *test2="/bin/test2";
	if(fd=open(test1,O_RDWR,0777)<0)
	{
		printf("open %s faild!\n ",test1);
	}
		printf("%s fd is %d ",test1,fd);
	
	if(fd=open(test2,O_RDWR|O_CREAT,0777)<0)
	{
		printf("open %s faild!\n ",test2);
	}
		printf("%s fd is %d ",test2,fd);
}

close

头文件和函数接口
 #include <unistd.h>
 int close(int fd);
  • fd为要关闭的文件描述符。
  • 关闭成功时返回0,出错时返回EOF.。
  • 程序在结束时会自动关闭所有打开的文件。
  • 文件被关闭后,再对文件进行任何操作都是无意义的。

read

头文件和函数接口
  #include <unistd.h>
  ssize_t read(int fd, void *buf, size_t count);
参数意义
fd即将读取文件的文件描述符
buf存储读入数据的缓冲区
count将要读入的数据的个数
ssize_t成功时返回读取的字节数,出错时返回EOF,读到文件末返回0

write

头文件和函数接口
  #include <unistd.h>
  ssize_t write(int fd, const void *buf, size_t count);
参数意义
fd即将读取文件的文件描述符
buf要写入的数据缓冲区
count写入数据的个数,大小不应该大于buf大小
ssize_t成功时返回写入的字节数,出错时返回EOF,读到文件末返回0

测试举例

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

#define MAX_SIZE 1000
 int main()
{
	int fd;
	char *testwrite = "/bin/testwrite";
	ssize_t length_w,ret,length_r=MAX_SIZE;
	char buffer_write[] = "Hello Write Function!\n ";
	char buffer_read[MAX_SIZE];
	if((fd = open(testwrite, O_RDWR|O_CREAT,0777))<0){
		printf("open %s failed\n",testwrite); 
	}
	//将buffer写入fd文件
	length_w = write(fd,buffer_write,strlen(buffer_write));
	if(length_w == -1)
	{
		perror("write");
	}
	else{
		printf("Write Function OK!\n");
	}
	close(fd);
	if((fd=open(testwrite,O_RDWR|O_CREAT,0777))<0){
	printf("open %s faild!\n ",testwrite);
	}	
	if((ret=read(fd,buffer_read,length_r))<0)
	{
		perror("read");
	}
	printf("file content is %s \n",buffer_read);
	close(fd);
}

lseek

头文件和函数接口
   #include <sys/types.h>
   #include <unistd.h>
   off_t lseek(int fd, off_t offset, int whence);
参数意义
fd文件描述符
offset偏移量,可正可负
whence指定一个基准点,基准点+偏移量等于当前位置
off_t成功时返回0,出错时返回EOF

whence参数可以为:SEEK_SET(文件开头),SEEK_CUR(当前输入输出位置),SEEK_END(文件末尾).

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Liangtao`

请作者喝杯咖啡吧~

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

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

打赏作者

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

抵扣说明:

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

余额充值