欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 文件IO

文件IO

2025/2/23 22:49:06 来源:https://blog.csdn.net/weixin_68641081/article/details/141156842  浏览:    关键词:文件IO

文件IO

  • 文件IO
    • 概念
    • 特性
    • 函数接口
      • open函数
      • write函数
      • read函数
      • lseek函数
  • 总结

文件IO

概念

操作系统为了方便用户使用系统功能而对外提供了一组系统函数,叫系统调用 ,其中有个叫文件IO;

文件IO一般都是对设备文件操作,当然也可以对普通文件进行操作;

它是一个基于Linux内核的没有缓存的IO机制;

特性

1.没有缓存区;
2.操作对象不在是流,而是文件描述符 FILE* int 0-1023;
3.文件描述符是一个很小的非负的整数,范围是0~1023,2024个;
内核每打开一个文件就会获得一个文件 描述符

              三个描述符与流对象匹配  0 —>STDIN_FILENO == stdin  1 —>STDOUT_FILENO == stdout  2 —>STDERR_FILENO == stderr

文件打开模式
fopen与open:
w
O_WRONLY|O_CREAT|O_TRUNC.

w+
O_RDWR|O_CREAT|O_TRUNC
r
O_RDONLY
r+ .
O_RDWR
a
O_WRONLY|O_CREAT|O_APPEND
a+
O_RDWR|O_CREAT|O_APPEND

函数接口

open函数

函数原型

int open(const char *pathname, int flags,int mode);
eg:open("1.c",O_WRONLY|O_CREAT,0666 );

功能:
获得一个文件描述符;
参数:
pathname:—文件名;
flags:
O_RDONLY —读权限
O_WRONLY—写权限
O_RDWR—读写权限
O_CREAT—创建文件(有这个创建文件时要写出mode的值)
O_EXCL—需要和O_CREAT同时使用,表示新建的文件不能存在,成功,否则open就会失败
O_NOCTTY—不是终端设备
O_TRUNC—文件内容清空
O_APPEND—追加
O_ASYNC—异步io,什么时候io不确定,
O_NONBLOCK—非阻塞
返回值:
成功返回文件描述符;
失败返回-1;
示例代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{int fd = open("2.txt",O_WRONLY| O_CREAT|O_TRUNC,0666);    if(-1 == fd){fprintf(stderr,"open error\n");return 1;}printf("fd is %d\n",fd);return 0;
}

write函数

函数原型

char buf[50];
ssize_t write(int fd,  const  void *buf, size_t count);

功能:
通过文件描述符向文件中写一串数据;
参数:
fd:文件描述符;
buf:要写入文件的字符串的首地址;
ount:要写入字符的个数;
返回值:
成功返回实际写入的个数;
失败返回-1;
示例代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{int fd = open("2.txt",O_WRONLY| O_CREAT|O_TRUNC,0666);    if(-1 == fd){fprintf(stderr,"open error\n");return 1;}printf("fd is %d\n",fd);char buf[512]="helloworld";int ret = write(fd,buf,strlen(buf));if(-1 == ret){fprintf(stderr,"write error\n");return 1;}close(fd);return 0;
}

read函数

函数原型

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

功能:
通过文件描述符读取文件中的数据;
参数:
fd:文件描述符;
buf:存放数据空间的首地址;
count:要读到数据的个数;
返回值:
成功返回读到数据的个数;
失败返回-1;
读到文件结尾返回0;
示例代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{int fd = open("2.txt",O_RDONLY);    if(-1 == fd){fprintf(stderr,"open error\n");return 1;}printf("fd is %d\n",fd);char buf[512]={0};while(1){int ret = read(fd,buf,sizeof(buf));if(ret<=0){break;}printf("%s\n",buf);}close(fd);return 0;
}

lseek函数

函数原型

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

功能:
定位光标的位置;
参数:
fd:文件描述符;

offset:
偏移量
正数:向后偏移;
负数:向前偏移;
零:不偏移;

whence:
SEEK_SET
SEEK_CUR
SEEK_END
返回值:
成功返回偏移量;
失败返回-1;
示例代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{int fd = open("2.txt",O_RDWR);    if(-1 == fd){fprintf(stderr,"open error\n");return 1;}off_t off = lseek(fd,15,SEEK_SET);printf("off %ld\n",off);char buf[]="hello";write(fd,buf,strlen(buf));printf("fd is %d\n",fd);close(fd);return 0;
}

总结

注意事项:
1.在write函数中count应该写buf里的字符串实际有效长度,不能一味的sizeof(buf);在read函数中count的值可以比实际的有效长度长。
2.在open函数中有创建的功能,此时要写mode的实参,例如0666,它的前导零是不能省略的,同时在创建目录和文件时,系统会默认的与umask相减,以便控制新建目标和文件有合理的权限。
重点:

用read,write复制文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{if(argc<3){fprintf(stderr,"usage:./a.out srcfile dstfile\n");return 1;}int src_fd = open(argv[1],O_RDONLY);    int dest_fd = open(argv[2],O_WRONLY| O_CREAT|O_TRUNC,0666);    if(-1 == src_fd ||-1 == dest_fd){fprintf(stderr,"open error\n");return 1;}while(1){char buf[512]={0};int red_ret = read(src_fd,buf,sizeof(buf));if(red_ret<=0){break;}write(dest_fd,buf,rd_ret);}close(dest_fd);close(src_fd);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词