Linux教程

Linux系统编程学习笔记——文件操作

本文主要是介绍Linux系统编程学习笔记——文件操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

打开/创建文件

open / creat

包含三个头文件:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

open函数的函数原型:

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

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

Flags:文件打开权限

O_RDONLY ——只读打开;

O_WRONLY ——只写打开;

O_RDWR ——可读可写打开;

返回值:一个非负整数的文件标识符;

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

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

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

O_EXCL 如果同时指定了OCREAT,而文件已经存在,则打开文件失败,返回值是-1

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

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

creat函数原型:

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

练习:

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

int main()
{
        int fd;

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

        if(fd == -1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }

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

        return 0;
}

写文件

写完文件后一定要close

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

int main()
{
        int fd;
        char *buf = "qieerbushe";

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

        if(fd == -1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }

        printf("open file1 success : fd = %d\n",fd);

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

        close(fd);

        return 0;
}

文件的读取

read——在fd指向的文件中读取count个字节的数据放到buf中,返回值:如果成功了,读到多少个字节返回多少;读到文件尾或者什么都读不到返回0,读取失败了返回-1。

练习 :

#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 = "qieerbushe";

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

        if(fd == -1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }

        printf("open file1 success : 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 %d byte to file\n",n_write);
        }

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write + 1);

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

        int n_read = read(fd,readBuf,n_write);
        printf("read %d ,context:%s\n",n_read,readBuf);

        close(fd);

        return 0;
}

运行结果:

 可以看到读到的字节是0,原因是在write后光标移动到了文件尾部,在read时光标位置不变,后面都是空的所以读不到。

解决办法:1.在write后关闭文件,光标会自动移到文件头,然后重新打开来read

2.移动光标

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

offset 为负数是往前读,为正数往后

SEEK_SET——指向文件头

SEEK_CUR——指向光标当前位置

SEEK_END——指向文件尾

int main()
{
        int fd;
        char *buf = "qieerbushe";

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

        if(fd == -1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }

        printf("open file1 success : 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 %d byte to file\n",n_write);
        }

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write + 1);

        //off_t lseek(int fd, off_t offset, int whence);
        lseek(fd,- n_write,SEEK_CUR);

        //   ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read %d ,context:%s\n",n_read,readBuf);

        close(fd);

        return 0;
}


O_EXCL判断文件是否存在

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

int main()
{
        int fd;

        fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
        if( fd == -1){

                printf("fd = %d\n",fd);
                printf("file cunzai\n");
        }
        return 0;
}
           

O_APPEND

配合 write使用,写入到文件尾,不会覆盖掉原来的字段

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

int main()
{
        int fd;
        char *buf = "qieerbushe";

        fd = open("./file1",O_RDWR|O_APPEND);

        printf("open file1 success : fd = %d\n",fd);

        int n_write = write(fd,buf,strlen(buf));

        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }

        close(fd);

        return 0;
}

O_TRUNC

配合write使用,会将原先文件里的字节删除再写入新的字节

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

int main()
{
        int fd;
        char *buf = "test";

        fd = open("./file1",O_RDWR|O_TRUNC);

        printf("open file1 success : fd = %d\n",fd);

        int n_write = write(fd,buf,strlen(buf));

        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }

        close(fd);

        return 0;
}

创建文件CREAT函数

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

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

mode:创建模式(可读可写可执行)

常见的创建模式:

宏表示数字
S_IRUSR4可读
S_IWUSR2可写
S_IXUSR1可执行
S_IRWXU7可读可写可执行

这篇关于Linux系统编程学习笔记——文件操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!