文件IO之open、read、close、write

一、文件IO之 open函数

1.函数原型

        int open(const char *pathname,int flags);//直接打开某文件,文件不存在时报错

        int open(const char *pathname,int flags,mode_t mode);//当中间参数为创建文件时,需要给文件权限

2.使用命令 man 2 open可查看函数原型以及其所需头文件

        #include <sys/types.h>

        #include <sys/stat.h>

        #include <fcntl.h>

3.open函数的简单使用

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

        int main(int argc,char *argv[])
        {
                int fd;
                fd=open("a.c",O_CREAT|O_RDWR,0666);//创建a.c的文件,并赋予0666权限

                if(fd<0){
                        printf("open is error\n");
                }

                printf("fd is %d\n",fd);

                return 0;
        }

二、文件IO之 close函数

1.函数原型

        int close(int fd);

2.使用命令 man 2 close可查看函数原型以及其所需头文件

        #include <unistd.h>

3.close函数的简单使用

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

        #include <unistd.h>

        int main(int argc,char *argv[])
        {
                int fd;
                fd=open("a.c",O_CREAT|O_RDWR,0666);//打开a.c文件,若没有则创建a.c文件

                if(fd<0){
                        printf("open is error\n");
                }

                printf("fd is %d\n",fd);

                close(fd);

                return 0;
        }

三、文件IO之 read函数

1.函数原型

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

2.使用命令 man 2 read可查看函数原型以及其所需头文件

        #include <unistd.h>

3.read函数的简单使用       

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

        int main(int argc,char *argv[])
        {
                int fd;
                fd=open("a.c",O_CREAT|O_RDWR,0666);

                char buf[32] = {0};

                ssize_t ret;
                if(fd<0){
                        printf("open is error\n");
                }

                printf("fd is %d\n",fd);
                ret = read(fd,buf,32);
                printf("ret=%ld\n",ret);
                printf("buf=%s\n",buf);
                close(fd);
                return 0;
        }

四、文件IO之 write函数

1.函数原型

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

2.使用命令 man 2 write可查看函数原型以及其所需头文件

        #include <unistd.h>

3.write函数的简单使用     

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

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


                fd=open("a.c",O_CREAT|O_RDWR,0666);

                if(fd<0){
                        printf("open is error\n");
                }

                printf("fd is %d\n",fd);

                write(fd,"aaa\n",6);//向a.c中写入aaa
                close(fd);
                return 0;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值