文件IO操作

目录

一、linux系统文件IO有哪些

二、使用步骤

1.open操作

2.write

 3.read

4.lseek

5.creat

linux操作总结

linux 额外补充


一、linux系统文件IO有哪些

文件IO口主要学习  open lseek read write close 

都是字面上的意思了 直接看操作叭

二、使用步骤

1.open操作

函数原型

int open(const char *pathname(指定文件路径), int flags(权限))

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
    int fd;
    fd = open("./file.txt",O_RDWR);
    printf("fd = %d\n",fd);


    return 0;
}

 成功

失败

 

返回函数用于打开指定路径下的文件并返回文件描述符。文件描述符是每个进程的一部分,它用于标识该进程访问文件的唯一文件描述符。

flags参数指定了打开文件的模式,例如读、写、追加等。Flags:

O_RDONLY只读打开

O_WRONLY 只写打开

O_RDWR 可读可写打开

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

以上这三个常数中应当只指定一个。

下列常数是可选择的:

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

O_EXCL如果同时指定了OCREAT,而文件已经存在,则出错。O_APPEND 每次写时都加到文件的尾端。O_TRUNC属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

int open(const char *pathname, int flags, mode_t mode)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
    int fd;
    fd = open("./file.txt",O_RDWR);
    if (fd> 0)
    {
        printf("file.txt success\n");
    }
    
    if(fd == -1){
        printf("open failed\n");
        fd = open("./file.txt",O_RDWR|O_CREAT,0600);//可读可写 4+2
        if (fd > 0)
        {
            printf("creat failed\n");
        }
        

    }

    return 0;
}

运行创建failed程序

用于打开指定路径下的文件,并以特定的模式(例如读、写、追加等)打开文件。与前一个函数不同的是,它在参数列表中多了mode_t类型的参数mode,表示新建文件时的访问权限。

int creat(const char *pathname, mode_t mode)

函数用于创建一个新文件或截断一个已存在的文件,然后以只写方式打开该文件。如果文件不存在,则会创建它。mode参数表示新建文件时的访问权限。注意,此函数已被淘汰,应该使用open()函数来代替。

2.write

需要引入的库文件

 #include <unistd.h>

函数原型

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

得先弄一个指针存内容 char *buf = "takagisang";

代码如下(示例):

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
    int fd;
    char *buf = "takagisang";
    fd = open("./file.txt",O_RDWR);
    if (fd> 0)
    {
        printf("file.txt success\n");
    }
    
    if(fd == -1){
        printf("open failed\n");
        fd = open("./file.txt",O_RDWR|O_CREAT,0600);//可读可写可执行
        if (fd > 0)
        {
            printf("creat failed\n");
        }
    }
    printf("file the fd = %d\n",fd);

//ssize_t write(int fd, const void *buf, size_t count); 返回写入大小

    write(fd,buf,strlen(buf));

    close(fd);

    return 0;
}

运行成功后在文件里的写入数据


 3.read

引入的库 #include <unistd.h>
函数原型 ssize_t read(int fd, void *buf, size_t count);

成功返回 字节数 失败返回 -1

read()函数是Unix/Linux操作系统中用于从文件描述符(fd)所代表的文件或设备读取数据的函数。它的声明在<unistd.h>头文件中。

ssize_t read(int fd, void *buf, size_t count)中的第一个参数fd为文件描述符,表示要读取数据的源文件或设备。第二个参数buf为指向接收读取数据的缓冲区起始地址的指针,第三个参数count为要读取的字节数。

函数返回成功读取的字节数,如果函数已达到文件末尾,则返回0表示读取结束。如果出错则返回−1并设置errno变量。

需要注意的是,read()函数会阻塞进程直到读取完指定字节数或者遇到错误或文件末尾为止。如果文件描述符使用了非阻塞模式,则可能读取到的字节数少于指定数量。

代码如下(示例):

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int fd;
    char *buf = "takagisang";
    fd = open("./file.txt", O_RDWR);
    if (fd > 0)
    {
        printf("file.txt success\n");
    }
    if (fd == -1)
    {
        printf("open failed\n");
        fd = open("./file.txt", O_RDWR | O_CREAT, 0600); // 可读可写可执行
        if (fd > 0)
        {
            printf("creat failed\n");
        }
    }
    printf("file the fd = %d\n", fd);
    // ssize_t write(int fd, const void *buf, size_t count); 返回写入大小
    int n_write = write(fd, buf, strlen(buf));
    if (n_write != -1)
    {
        printf("write failed cusser\n");
    }
    close(fd); //先不用lseek 移动光标 用关闭打开的方式移动
    fd = open("./file.txt", O_RDWR);
    char *readbuf;
    readbuf = (char *)malloc(n_write + 1);
    read(fd, readbuf, n_write);
    printf("read %d bytes %s\n", n_write, readbuf);
    close(fd);

    return 0;
}

在window运行 

出现乱码应该与读取txt的方式有关在linux下就没有这个情况

 在linux 下运行

4.lseek

#include <sys/types.h>

#include <unistd.h>

off_t lseek(int fd, off_t offset, int whence);

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int fd;
    char *buf = "takagisang";
    fd = open("./file.txt", O_RDWR);
    if (fd > 0)
    {
        printf("file.txt success\n");
    }
    if (fd == -1)
    {
        printf("open failed\n");
        fd = open("./file.txt", O_RDWR | O_CREAT, 0600); // 可读可写可执行
        if (fd > 0)
        {
            printf("creat failed\n");
        }
    }
    printf("file the fd = %d\n", fd);
    // ssize_t write(int fd, const void *buf, size_t count); 返回写入大小
    int n_write = write(fd, buf, strlen(buf));
    if (n_write != -1)
    {
        printf("write failed cusser\n");
    }

    lseek(fd, 0, SEEK_SET);//用lseek来操作光标

    char *readbuf;
    readbuf = (char *)malloc(n_write + 1);
    read(fd, readbuf, n_write);
    printf("read %d bytes %s\n", n_write, readbuf);
    close(fd);

    return 0;
}

lseek()函数是Unix/Linux操作系统中用于移动文件读写指针的函数。它的声明在 <sys/types.h><unistd.h>头文件中。

off_t lseek(int fd, off_t offset, int whence)中的第一个参数fd为文件描述符,表示要移动读写指针的文件或设备。第二个参数offset为要移动的字节数,这个可以为正数(向文件尾偏移)或负数(向文件头偏移)。第三个参数whence则规定了移动偏移量的起始点,可能取值为SEEK_SET(从文件头开始移动)、SEEK_CUR(从当前位置开始移动)、SEEK_END(从文件尾开始移动)。

函数返回新的读写指针的偏移量。如果出错则返回-1并设置errno变量。

需要注意的是,lseek()函数只能用于打开支持random access的文件和设备(例如硬盘文件、闪存设备、磁带等),而不能用于打开支持顺序访问的设备(例如终端设备)。

 lseek()函数的返回值是当前文件读写指针相对于文件开始处的偏移量。(常用)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int fd;
    char *buf = "takagisang";
    fd = open("./file.txt", O_RDWR);

    int fileSize =  lseek(fd, 0, SEEK_END);//用lseek来操作光标 第二参数为左减右加原则 返回偏移值 

    close(fd);

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

    return 0;
}

5.creat

int creat (const char *filename,mode t mode)

filename:要创建的文件名(包含路径、缺省为当前路径)

mode:创建模式//可读可写可执行常见创建模式:

宏表示数字可读S_IRUSR  4

可写   S_IWUSR  2

可执行   S_IXUSR 1

可读、写、执行S_IRWXU 7

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int fd;
    char *buf = "takagisang";

    creat("E:/share/linux/tgk",S_IRWXU);
    return 0;
}

 即可在E:/share/linux/下生成一个tgk文件

linux操作总结

1 、-l 用于查看文件的权限


linux 额外补充

open权限       

   当我们附带了权限后,打开的文件就只能按照这种权限来操作。以上这三个常数中应当只指定一个。下列常数是可选择的:

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

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

O_APPEND每次写时都加到文件的尾端。O_TRUNC属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。另起一行加入写入的内容。用O_RDWR会覆盖原来内容。

O_TRUNC属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。把原先文本干掉,然后重新写入新文本。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值