文件IO

文件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;
}
  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值