文件IO——open、creat、close、write、read的使用

目录

一、函数头文件介绍

二、 打开文件函数open

1.open函数解析

2.open函数例程

三、创建文件函数creat

1.creat函数解析

2.creat函数例程

四、关闭文件函数close

五、写文件函数write

1.write函数解析

2.write函数例程

六、读文件函数read

1.read函数解析

2.read函数例程


一、函数头文件介绍

        在所有的 linux 系统中,如果需要对文件的进行操作,只要包含如下 4 个头文件即可。 

        #include <unistd.h>

        #include <sys/types.h>        

        #include <sys/stat.h>

        #include <fcntl.h>

       上面四个头文件中包含了打开,关闭,创建,读文件,写文件的函数,还有标志位,以及在不同 32 位以及 64 位系统下数据长度的宏变量定义。 

二、 打开文件函数open

1.open函数解析

        使用 open 函数的时候会返回一个文件句柄,文件句柄是文件的唯一识别符 ID。对文件的操作必须从读取句柄开始。

        open 的两个原型:

int open(const char *path, int oflags);
int open(const char *path, int oflags,mode_t mode);

        第一个参数 path 表示:路径名或者文件名。路径名为绝对路径名,例如开发板中的 led驱动的设备几点/dev/leds。

        第二个参数 oflags 表示:打开文件所采取的动作。

        下面三个选项是必须选择其中之一的。

        O_RDONLY 文件只读

        O_WRONLY 文件只写

        O_RDWR 文件可读可写

        下面是可以任意选择的。

        O_APPEND 每次写操作都写入文件的末尾O_CREAT 如果指定文件不存在,则创建这个文件
        O_EXCL 如果要创建的文件已存在,则返回 -1,并且修改 errno 的值
        O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容
        O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
        O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O
设置为非阻塞模式(nonblocking mode),后面会介绍什么是阻塞和非阻塞。
        O_NDELAY 和 O_NONBLOCK 功能类似,调用 O_NDELAY 和使用的
        O_NONBLOCK 功能是一样的。

        第三个参数 mode 表示:设置创建文件的权限。S_IRUSR,S_IWUSER,S_IXUSR,S_IRGRP,S_IWGRP,S_IXGRP,S_IROTH,S_IWOTH,S_IXOTH.

        其中 R:读,W:写,X:执行,USR:文件所属的用户,GRP:文件所属的组,OTH:其他用户。第三个参数可以直接使用参数代替。

2.open函数例程

#include <stdio.h>
#include <sys/types.h>      //man 2 open
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd;
    char *leds = "/dev/leds";
    char *test1 = "/bin/test1";

    if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
    {
        printf("open %s failed!\n",leds);
    }
    else
    {
        printf("%s fd is %d\n",leds,fd);
    }

    if((fd = open(test1,O_RDWR|O_CREAT,0777)) < 0)
    {
        printf("open %s failed!\n",test1);
    }
    else
    {
        printf("%s fd is %d\n",test1,fd);
    }

    return 0;
}

三、创建文件函数creat

1.creat函数解析

creat 函数原型如下:

int creat(const char * pathname, mode_t mode);

creat 函数只有两个参数,参数的含义和 open 类似。

大家看到这个函数的时候知道它是创建文件的就成,在写代码的时候完全可以用 open 代替。

2.creat函数例程

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

int main()
{
    int fd;
    char *leds = "/dev/leds";
    char *test1 = "/bin/test1";
    char *test2 = "/bin/test2";

    if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
    {
        printf("open %s failed!\n",leds);
    }
    else
    {
        printf("%s fd is %d\n",leds,fd);
    }

    if((fd = open(test1,O_RDWR|O_CREAT,0777)) < 0)
    {
        printf("open %s failed!\n",test1);
    }
    else
    {
        printf("%s fd is %d\n",test1,fd);
    }

    fd = creat(test2,0777);
    if(fd == -1)
    {
        printf("%s fd is %d\n",test2,fd);
    }
    else
    {
        printf("create %s is succeed\n",test2);
    }

    return 0;
}

四、关闭文件函数close

        close 函数在头文件“#include <unistd.h>”中,close 函数的使用和参数都比较简单.

        int close(int fd);

        参数 fd,使用 open 函数打开文件之后返回的句柄。返回值,一般很少使用 close 的返回值。

五、写文件函数write

1.write函数解析

write 函数在头文件“#include <unistd.h>”中。

函数原型为 ssize_t write(int fd,const void *buf,size_t count)

参数 fd,使用 open 函数打开文件之后返回的句柄。

参数*buf,需要写入的数据。

参数 count,将参数*buf 中最多 count 个字节写入文件中。

返回值为 ssize 类型,出错会返回-1,其它数值表示实际写入的字节数。

2.write函数例程

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

int main()
{
    int fd;
    char *testwrite = "/bin/testwrite";
    ssize_t length_w;
    char buffer_write[] = "hello meng!";

    fd = open(testwrite,O_RDWR|O_CREAT,0777);
    if(fd < 0){
        printf("open %s failed\n",testwrite);
    }

    length_w = write(fd,buffer_write,strlen(buffer_write));
    if(length_w == -1){
        perror("write");
    }else{
        printf("OK\n");
    }

    close(fd);

    return 0;
}

六、读文件函数read

1.read函数解析

read 函数在头文件“#include <unistd.h>”中。

函数原型为 ssize_t read(int fd,void *buf,size_t len)参数 fd,使用 open 函数打开文件之后返回的句柄。

参数*buf,读出的数据保存的位置。

参数 len,每次最多读 len 个字节。

返回值为 ssize 类型,出错会返回-1,其它数值表示实际写入的字节数,返回值大于 0 小于 len 的数值都是正常的。

2.read函数例程

#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,length_r = MAX_SIZE,ret;
    char buffer_write[] = "hello meng!";

    char buffer_read[MAX_SIZE];

    fd = open(testwrite,O_RDWR|O_CREAT,0777);
    if(fd < 0){
        printf("open %s failed\n",testwrite);
    }

    length_w = write(fd,buffer_write,strlen(buffer_write));
    if(length_w == -1){
        perror("write");
    }else{
        printf("OK\n");
    }

    close(fd);

    fd = open(testwrite,O_RDWR|O_CREAT,0777);
    if(fd < 0){
        printf("open %s failed\n",testwrite);
    }
    if(ret = read(fd,buffer_read,length_r)){
        perror("read");
    }else{
        printf("content is %s \n",buffer_read);
    }
    close(fd);

    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux中,可以使用系统调用进行文件拷贝操作。以下是一个使用系统I/O实现文件拷贝的示例代码: ```c #include <fcntl.h> #include <unistd.h> #define BUF_SIZE 4096 int main(int argc, char *argv[]) { int source_fd, dest_fd; ssize_t bytes_read, bytes_written; char buffer[BUF_SIZE]; // 检查命令行参数 if (argc != 3) { printf("Usage: %s <source_file> <destination_file>\n", argv[0]); return 1; } // 打开源文件 source_fd = open(argv[1], O_RDONLY); if (source_fd == -1) { perror("open"); return 1; } // 创建目标文件 dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (dest_fd == -1) { perror("open"); close(source_fd); return 1; } // 从源文件读取数据并写入目标文件 while ((bytes_read = read(source_fd, buffer, BUF_SIZE)) > 0) { bytes_written = write(dest_fd, buffer, bytes_read); if (bytes_written != bytes_read) { perror("write"); close(source_fd); close(dest_fd); return 1; } } // 关闭文件描述符 close(source_fd); close(dest_fd); return 0; } ``` 你可以将上述代码保存为一个.c文件(例如copy.c),然后通过以下命令编译并运行: ``` gcc copy.c -o copy ./copy <source_file> <destination_file> ``` 其中,`<source_file>`是源文件的路径,`<destination_file>`是目标文件的路径。注意替换这两个占位符为你实际的文件路径。 这段代码使用了`open`、`read`和`write`等系统调用来进行文件的打开、读取和写入操作,通过循环逐块拷贝数据,直到源文件读取完毕。请确保在运行代码时有足够的权限来读取和写入文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

琪琪猫不会嵌入式

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

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

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

打赏作者

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

抵扣说明:

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

余额充值