day02(IO进程)文件IO

 目录

一.什么是文件IO

1.1 概念

1.2 特点

1.3 操作

二.函数接口

2.1 打开文件open()

2.2 关闭文件 close()

2.3 读写文件

2.3.1 读文件 read()

2.3.2 写文件 write()

练习:cp功能

2.4 文件定位操作

标准IO和文件IO总结

练习: 实现“head -n 文件名”命令的功能


一.什么是文件IO

1.1 概念

在posix(可移植操作系统接口)中定义的一组输入输出的函数

文件IO又称系统IO,是系统调用,是操作系统提供的接口函数。

POSIX接口 (英语:Portable Operating System Interface)可移植操作系统接口

1.2 特点

  1. 没有缓冲机制,每次都会引起系统调用。
  2. 围绕文件描述符进行操作,非负整数(>=0),依次分配。
  3. 文件IO默认打开三个文件描述符,分别是0(标准输入)、1(标准输出)和2(标准错误)。
  4. 操作除了目录d以外任意类型的文件b c - l s p
  5. 可移植性相对较差

问题:打开三个文件, 描述符分别是:3 4 5

关闭4以后,重新打开这个文件,描述符是几?

答: 还是4

问题:一个进程的文件描述符最大到几?最多能打开多少个文件描述符?最多能打开多少个文件?

答: 一个进程的文件描述符最大到1023 (0-1023), 最多能打开1024个文件描述符, 最多能打开1021(1024-3)个文件。

1.3 操作

文件文件:open

关闭文件: close

读写操作: read和write

定位操作:lseek

二.函数接口

2.1 打开文件open()

man 2 open

O_RDONLY、O_WRONLY、O_RDWR、O_CREAT、O_TRUNC、O_APPEND

w: O_WRONLY | O_CREAT | O_TRUNC

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

int open(const char *pathname, int flags);
功能:打开文件
参数:pathname:文件路径名
     flags:打开文件的方式
            O_RDONLY:只读
            O_WRONLY:只写
            O_RDWR:可读可写
            O_CREAT:不存在创建
            O_TRUNC:存在清空
            O_APPEND:追加   
返回值:成功:文件描述符
       失败:-1

当第二个参数中有O_CREAT选项时,需要给open函数传递第三个参数,指定创建文件的权限 
int open(const char *pathname, int flags, mode_t mode);
最后权限 = mode &(~umask)  
例如:指定权限为0666(8进制)
最终权限= 0666 & (~umask) = 0666 &(~0002) = 664

 666  => 110 110 110
&775     111 111 101
 664 	  110 110 100

思考:文件IO和标准IO的打开方式的对应关系

标准IO

文件IO

r

O_RDONLY

只读

r+

O_RDWR

可读可写

w

O_WRONLY | O_CREAT | O_TRUNC, 0777

只写,不存在创建,存在清空

w+

O_RDWR | O_CREAT | O_TRUNC, 0777

可读可写,不存在创建,存在清空

a

O_WRONLY | O_CREAT | O_APPEND, 0777

只写,不存在创建,存在追加

a+

O_RDWR | O_CREAT | O_APPEND, 0777

可读可写,不存在创建,存在追加

注意:O_CREAT需要指定第三个参数表示权限

2.2 关闭文件 close()

#include <unistd.h>
int close(int fd);
功能:关闭文件
参数:fd:文件描述符
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    int fd;
    //1.打开文件
    //fd = open("a.c", O_RDONLY); //r
    fd = open("a.c", O_WRONLY | O_CREAT | O_TRUNC, 0666); //w
    if (fd < 0)
    {
        perror("open err ");
        return -1;
    }
    printf("fd: %d\n", fd);

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

2.3 读写文件

2.3.1 读文件 read()

ssize_t read(int fd, void *buf, size_t count);
功能:从一个已打开的可读文件中读取数据
参数: fd  文件描述符
      buf  存放位置
      count  期望的个数
返回值:成功:实际读到的个数(小于期望值说明实际没这么多)
       返回0:表示读到文件结尾
       返回-1:表示出错,并设置errno号

2.3.2 写文件 write()

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
功能:向指定文件描述符中,写入 count个字节的数据。
参数:fd   文件描述符
      buf   要写的内容
      count  期望写入字节数
返回值:成功:实际写入数据的个数
              失败  : -1

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

int main(int argc, char const *argv[])
{
    int fd;
    char buf[32] = "";
    //1.打开文件
    fd = open("a.c", O_RDWR); //r+
    if (fd < 0)
    {
        perror("open err ");
        return -1;
    }
    printf("fd: %d\n", fd);

    //2. 读写文件
    read(fd, buf, 10);  //文件中内容为:hello
    printf("%s\n", buf); //hello

    write(fd, "world", 5);

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

练习:cp功能

文件IO实现cp功能。cp 源文件 新文件名

./a.out src dest

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

int main(int argc, char const *argv[])
{
    int fd1, fd2;
    if (argc != 3)
    {
        printf("err: %s <srcfile> <destfile>\n", argv[0]);
        return -1;
    }

    //1.打开两个文件
    fd1 = open(argv[1], O_RDONLY);
    if (fd1 < 0)
    {
        perror("fd1 open err");
        return -1;
    }
    fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0777);
    if (fd2 < 0)
    {
        perror("fd2 open err");
        return -1;
    }

    //2.循环读源文件,只要读到就写入目标
    char buf[32] = "";
    ssize_t n;
    while ((n = read(fd1, buf, 32)) > 0)
        write(fd2, buf, n);
    
    //while (read(fd1, buf, 1) > 0)  //或者
    //    write(fd2, buf, 1);

    //3. 关闭两个文件
    close(fd1);
    close(fd2);

    return 0;
}

2.4 文件定位操作

off_t lseek(int fd, off_t offset, int whence);
功能:设定文件的偏移位置
参数:fd:文件描述符
    offset: 偏移量  
        正数:向文件结尾位置移动
        负数:向文件开始位置
    whence: 相对位置
        SEEK_SET   开始位置
        SEEK_CUR   当前位置
        SEEK_END   结尾位置
补充:和fseek一样其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.

返回值:成功:文件的当前位置
        失败:-1
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    int fd;

    fd = open("a.c", O_RDWR);
    if (fd < 0)
    {
        perror("open err");
        return -1;
    }

    //离开头后10个
    lseek(fd, 10, SEEK_SET);
    write(fd, "k", 1);

    off_t off = lseek(fd, 0, SEEK_END);
    printf("%ld\n", off);

    return 0;
}


标准IO和文件IO总结

标准IO

文件IO

概念

在C库中定义的一组用于输入输出的函数

在posix中定义的一组用于输入输出的函数

特点

  1. 有缓冲机制
  2. 围绕流进行操作,FILE*
  3. 默认打开三个流:stdin/stdout/stderr
  4. 只能操作普通文件
  5. 可移植性较强
  1. 没有缓冲机制
  2. 围绕文件描述符进行操作,非负整数
  3. 默认打开三个文件描述符:0 1 2
  4. 可以操作除了目录以外任何类型文件
  5. 可移植性相对较弱

函数

打开文件:fopen/freopen

关闭文件: flose

读文件:fgetc/fgets/fread

写文件:fputc/fputs/fwrite

定位操作: rewind/fseek/ftell

打开文件: open

关闭文件: close

读文件: read

写文件: write

定位操作: lseek

练习: 实现“head -n 文件名”命令的功能

思路:循环读,读到就累加行数,并且打印到终端,判断是否达到最后一行,达到就退出。

实现“head -n 文件名”命令的功能

例:head -3 test.c -> ./a.out -3 test.c

atoi: "123" -> 123(整型)

argv[1]: "-3" 的首地址

argv[1]+1: "3" 的首地址

atoi(argv[1]+1) ==> 3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    if (argc != 3)
    {
        printf("err: %s -n <filename>\n", argv[0]); //./a.out -n <filename>
        return -1;
    }

    FILE *fp = fopen(argv[2], "r");
    if (fp == NULL)
    {
        perror("fopen err");
        return -1;
    }

    //获取行数
    int num = atoi(argv[1] + 1); // +1是为了向后偏移一个地址单位把-去掉获取到要打印的行数

    char buf[32] = "";
    int len = 0;

    //循环读,然后数换行,打印到终端,如果是最后一行就退出
    while (fgets(buf, 32, fp) != NULL)
    {
        if (buf[strlen(buf) - 1] == '\n')     //数换行
            len++;

        if (len > num)    //达到行数就退出
            break;

        fputs(buf, stdout); //打印内容到终端
    }
    fclose(fp);
    return 0;
}

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值