美文网首页
文件操作

文件操作

作者: 如果听见下雨的声音 | 来源:发表于2020-04-21 14:09 被阅读0次

声明:图片资源摘自于网络

1572612752969.png

创建文件

文件原子操作

  • O_APPEND标志,进程对文件偏移量调整和追加数据是原子操作
  • O_CREAT和O_EXCL,判定文件是否存在之后进行操作,文件创建和判定过程是原子操作

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

constexpr const char new_filename[] = "/tmp/x1";

void create_file()
{

/*    int ret;

    ret = access(new_filename, F_OK | R_OK | W_OK);

    if (ret < 0)
    {
        perror("Can't permission access");
    }*/

    int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);

    if (fd)
    {
        char str[] = "1234567890";

        write(fd, str, sizeof(str));

    }

    close(fd);

}

获取文件大小

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>


void get_file_size()
{

    int fd = open(new_filename, O_RDONLY | O_NDELAY, 0766);

    if (fd)
    {
        const long int &size = lseek(fd, 0, SEEK_END);

        char str[size];

        lseek(fd, 0, SEEK_SET);

        read(fd, str, size);

        printf("%s \n file size: %ld \n", str, size);
    }

    close(fd);

}

设置文件size

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

void set_file_size_by_seek()
{
    int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);

    if (fd)
    {
        char str[] = "1234567890";

        write(fd, str, sizeof(str));

        printf("str size:%ld", sizeof(str));

        lseek(fd, 99, SEEK_SET);

        write(fd, "\0", 1);
    }

    close(fd);

}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

void set_file_size_by_truncate()
{

    int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);

    if (fd)
    {
        char str[] = "1234567890";

        write(fd, str, sizeof(str));

        printf("str size:%ld", sizeof(str));

        ftruncate(fd, 1024);

    }

    close(fd);

}

获取文件的stat

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

void print_file_stat_struct()
{
    using FileStat = struct stat;

    size_t size = sizeof(FileStat);

    printf("size = %ld \n", size);

    FileStat fileStat{};

    lstat(new_filename, &fileStat);

    char _data[size];

    memcpy(_data, &fileStat, size);

    std::string data{_data, size};

    std::cout << data << std::endl;

    std::cout << fileStat.st_size << std::endl;
    std::cout << fileStat.st_gid << std::endl;
    std::cout << fileStat.st_uid << std::endl;
    std::cout << fileStat.st_ctim.tv_nsec << std::endl;
    std::cout << fileStat.st_ctim.tv_sec << std::endl;
    std::cout << fileStat.st_atim.tv_nsec << std::endl;
    std::cout << fileStat.st_atim.tv_sec << std::endl;
    std::cout << fileStat.st_blksize << std::endl;
    std::cout << fileStat.st_blocks << std::endl;
    std::cout << fileStat.st_dev << std::endl;
    std::cout << fileStat.st_mode << std::endl;
    std::cout << fileStat.st_nlink << std::endl;
    std::cout << fileStat.st_rdev << std::endl;

}

检测文件对当前用户操作的权限(access)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

void file_unlink_access()
{

    int ret;

    ret = access(new_filename, F_OK | R_OK | W_OK);

    if (ret < 0)
    {
        perror("Can't permission access\n");
    }

    int fd = open(new_filename, O_RDONLY);

    unlink(new_filename); //grant delete power of 目录项

    if (fd)
    {
        const long int &size = lseek(fd, 0, SEEK_END);

        char str[size];

        lseek(fd, 0, SEEK_SET);

        read(fd, str, size);

        printf("%s \n file size: %ld \n", str, size);

    }


    close(fd);

}

标准输入(0),标准输出(1),标准错误(2)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

void stdout_dev()
{
    printf("Main precess pid: %u \t thread id = %lu\n", getpid(), pthread_self());

    char str[] = "213542363457";

    write(STDOUT_FILENO, str, sizeof(str) - 1);
    
    printf("%d\n%d\n%d\n", STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO);
}

fcntl(修改已经打开文件的属性)

1572614202968.png

ioctl(驱动设备操作或外围设备操作)

1572614274071.png

dup和dup2(文件重定向)

1572614788016.png

lseek(定位文件位置)

1572632205025.png

link和unlink(创建硬连接和删除硬连接)

1572634803536.png

symlink和readlink(创建软连接和读取软连接)

1572634572946.png

相关文章

  • 文件操作

    文件操作 目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思...

  • 文件和目录处理相关

    文件和目录处理相关 题: 考点:文件操作/写入操作; 延伸:目录操作函数,其他文件操作; 文件读写操作 文件系统函...

  • 09-文件操作

    一、文件操作流程 a.普通文件操作流程: 打开文件 操作文件 关闭文件 b. json文件操作流程: open(文...

  • VBS文件操作

    VBS文件操作'操作文本文件,操作fso对象(文件对象操作) --------------------------...

  • 文件操作

    文件操作:打开文件、读写文件、操作文件内容 写入文件操作:(把大象装入冰箱)1.打开文件 ...

  • 类的补充

    一.复习 1.文件操作a.操作流程:打开文件(open),操作文件,关闭文件with open() as 文件变量...

  • 文件

    目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思考:什么是...

  • 16总 正则表达式

    复习: 1.文件操作a.操作流程: 打开文件(open) --- 操作文件 --- 关闭文件(close)with...

  • 2018-09-10

    01-recode 1.文件操作a.操作流程:打开文件---》操作文件----》关闭文件with open() ...

  • 2018-09-10 day16总结

    1.文件操作 a.操作流程:打开文件(open)-操作文件-关闭文件(close)with open() as 文...

网友评论

      本文标题:文件操作

      本文链接:https://www.haomeiwen.com/subject/nlyuihtx.html