Linux文件操作

目录

open函数

无权限打开(给open函数传递前两个参)

有权限打开

 write函数

read函数

重新打开文件去读取:

移动光标读取文件:(lseek)

利用lseek去自动计算一个文件有多少个字节:

Linux系统自带的文件描述符


类似于Windows打开创建文件,Linux的过程:

 

操作系统提供了一系列的API;如Linux系统:

  1. 打开 open

  2. 读写 write /read

  3. 光标定位 lseek

  4. 关闭 close

open函数

 

  • Pathname:要打开的文件名(含路径,缺省为当前路径)

  • Flags:

  • O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开

  • 当我们附带了权限后,打开的文件就只能按照这种权限来操作。

  • 以上这三个常数中应当只指定一 个。下列常数是可选择的:

  • O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。

  • O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。

  • O_APPEND 每次写时都加到文件的尾端。

  • O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

  • Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限

 

无权限打开(给open函数传递前两个参)

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

//要操作这些函数就要包含上面的三个头文件
#include <stdio.h>

int main()
{
	int fd;
	
	fd = open("./file1",O_RDWR);

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

	return 0;
}

上面代码演示当file1存在的情况下,当file1不存在时候:

有权限打开

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


int main()
{
        int flg;
        flg=open("./file1",O_RDWR);

        if(flg<1)
        {
                printf("open file1 failed! flag=%d\nI`m creating it!\r\n",flg);
                flg=open("./file1",O_RDWR|O_CREAT,0600);//0600是权限,也就是创建的这个文件可读可写
        }
        if(flg>=1)
        {
                printf("file1 open successed!\nflag=%d\n",flg);
        }
        return 0;
}

权限列表:

  1. 可读:r 4

  2. 可写:w 2

  3. 执行:x 1

上面的代码中0600表示可读可写(4+2);

 write函数

 

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

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

#include<unistd.h>

int main()
{
        int flg;
        char* buf="you are so cool!\r\n";
        flg=open("./file1",O_RDWR);

        if(flg<1)
        {
                printf("open file1 failed! flag=%d\nI`m creating it!\r\n",flg);
                flg=open("./file1",O_RDWR|O_CREAT,0600);//with this 0600,you can read and write this file
        }
        if(flg>=1)
        {
                printf("file1 open successed!\nflag=%d\n",flg);
        }



        //ssize_t write(int fd, const void *buf, size_t count);这个是write函数的接口定义
        write(flg,buf,strlen(buf));
        close(flg);
        return 0;
}

上面的代码在已经创建的文件file1中写入 you are so coll!\r\n,fd参数是open函数传递回来的整型文件标记,buf是要写入的字符串数据,size。。。。。。

read函数

 初次编写:::

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

#include<unistd.h>

int main()
{
        int flg;
        char* buf="you are so cool!\r\n";

        flg=open("./file1",O_RDWR);

        if(flg<1)
        {
                printf("open file1 failed! flag=%d\nI`m creating it!\r\n",flg);
                flg=open("./file1",O_RDWR|O_CREAT,0600);//with this 0600,you can read and write this file
        }
        if(flg>=1)
        {
                printf("file1 open successed!\nflag=%d\n",flg);
        }



        //ssize_t write(int fd, const void *buf, size_t count);
        //On success, the number of bytes written is returned.  On error,  -1  is
        // returned, and errno is set to indicate the cause of the error.

        int n_write=write(flg,buf,strlen(buf));
        printf("wtite messages to file1 success,and write %d byte!\r\n",n_write);



        char* readbuf;
        readbuf=(char*)malloc(n_write+1);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read=read(flg,readbuf,n_write);

        printf("read %d byte!\r\ncontext:%s!\r\n",n_read,readbuf);
        close(flg);
        free(readbuf);
        return 0;
}

上面代码执行结果:

 读取到0个数据??????

前面write函数在写入file1完成之后,光标就停在了文档末位位置。所以在read的时候,也是从末位位置开始read的。如果读取该文件,需要把光标移动到最开始的位置或者是重新打开这个文件。

重新打开文件去读取:

int main()
{
        int flg;
        char* buf="you are so cool!\r\n";

        flg=open("./file1",O_RDWR);

        if(flg<1)
        {
                printf("open file1 failed! flag=%d\nI`m creating it!\r\n",flg);
                flg=open("./file1",O_RDWR|O_CREAT,0600);//with this 0600,you can read and write this file
        }
        if(flg>=1)
        {
                printf("file1 open successed!\nflag=%d\n",flg);
        }


        //ssize_t write(int fd, const void *buf, size_t count);
        //On success, the number of bytes written is returned.  On error,  -1  is
        // returned, and errno is set to indicate the cause of the error.

        int n_write=write(flg,buf,strlen(buf));
        printf("wtite messages to file1 success,and write %d byte!\r\n",n_write);

        close(flg);					
        flg=open("./file1",O_RDWR);		//写入完成之后重新打开,这样文件光标就重新指向了文件开始位置。


        char* readbuf;
        readbuf=(char*)malloc(n_write+1);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read=read(flg,readbuf,n_write);

        printf("read %d byte!\r\ncontext:%s!\r\n",n_read,readbuf);
        close(flg);
        free(readbuf);
        return 0;
}

执行结果:

移动光标读取文件:(lseek)

lseek函数。

将文件读写指针相对whence移动offset个字节 。

 

 

int main()
{
        int flg;
        char* buf="you are so cool!\r\n";

        flg=open("./file1",O_RDWR);

        if(flg<1)
        {
                printf("open file1 failed! flag=%d\nI`m creating it!\r\n",flg);
                flg=open("./file1",O_RDWR|O_CREAT,0600);//with this 0600,you can read and write this file
        }
        if(flg>=1)
        {
                printf("file1 open successed!\nflag=%d\n",flg);
        }


        //ssize_t write(int fd, const void *buf, size_t count);
        //On success, the number of bytes written is returned.  On error,  -1  is
        // returned, and errno is set to indicate the cause of the error.

        int n_write=write(flg,buf,strlen(buf));
        printf("wtite messages to file1 success,and write %d byte!\r\n",n_write);

//      close(flg);
//      flg=open("./file1",O_RDWR);


        char* readbuf;
        readbuf=(char*)malloc(n_write+1);

//      off_t lseek(int fd, off_t offset, int whence);
//		直接把光标移动到文件的最开始位置。
        lseek(flg,0,SEEK_SET);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read=read(flg,readbuf,n_write);

        printf("read %d byte!\r\ncontext:%s\r\n",n_read,readbuf);
        close(flg);
        return 0;
}

参数whence:

SEEK_SET,光标移动到文件最开始;

SEEK_CUR,光标在当前位置不动;

SEEK_END,光标移动到文件最末尾;

参数offset:

偏移值,负数的话光标是往前走(左),正数光标往后走(右)。

 

利用lseek去自动计算一个文件有多少个字节:

 

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

#include<unistd.h>

int main()
{
        int flg;
        flg=open("./file1",O_RDWR);

//      off_t lseek(int fd, off_t offset, int whence);
        int fileSize=lseek(flg,0,SEEK_END);

        printf("file1 size is %d byte!\r\n",fileSize);
        close(flg);
        return 0;
}

当lseek执行成功时,它会返回最终以文件起始位置为起点的偏移位置。如果出错,则返回-1,同时errno被设置为对应的错误值。

 所以设置为SEEK_END,lseek会从头开始计算到末尾一共偏移多少字节。可以用来查询文档中占据多少个字节。

执行结果:

file1文件内容:

Linux系统自带的文件描述符

 

0:标准输入(从键盘输入)

1:标准输出(输出到控制台)

2:标准错误

可以利用这个实现一个类似C语言的scanf和printf的效果。

代码如下:

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

#include<unistd.h>

int main()
{
        char buf[128];
        int n_read=read(0,buf,128);

        //ssize_t read(int fd, void *buf, size_t count);
        write(1,buf,n_read);
        return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值