美文网首页
8.文件管理

8.文件管理

作者: 陈忠俊 | 来源:发表于2020-03-22 21:17 被阅读0次

1.cat命令

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define E_MSG(str, val) do{perror(str); return (val);}while(0)

int main(int argc, char *argv[]){
    char buf[128];
        int r;
        int fd = open(argv[1], O_RDONLY) ;
        if(fd == -1) E_MSG("OPEN", -1);
        while((r = read(fd, buf, 128)) > 0){
                write(1, buf, r);
        }
        close(fd);
        return 0;
}

  1. copy命令的实现
    输入文件通过argv[1],输出保存到argv[2]。若目标文件不存在,创建一个新的,存在,清空文件内容,并复制新的内容。
#include<stdio.h>
#include<file.h>

int cp_file(int src_fd, int dst_fd){
        int total = 0;
        int m_read, m_write;
        char buff[128];
        while((m_read = read(src_fd, buff, 128)) > 0){
                char *temp = buff;
                while(m_read > 0){
                        m_write = write(dst_fd, temp, m_read);
                        m_read = m_read - m_write;
                        total += m_write;
                }
        }
        return total;
}

int main(int argc, char *argv[]){
        //open a file with read only
        int src_fd = open(argv[1], O_RDONLY);
        //open dest file with write mode;
        //if the file not exits, create a new one with mode 644;
        //if the file exist, empty it
        int flags = O_WRONLY|O_CREAT|O_TRUNC;
        int dst_fd = open(argv[2], flags, 0644);
        if(src_fd == -1 | dst_fd == -1) return "-1";
        cp_file(src_fd, dst_fd);
        close(src_fd);
        close(dst_fd);
        return 0;
}

  1. 文件描述符的复制
    输入的文件通过argv[1]。文件不存在,创建一个新的,存在,清空文件内容。
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>

int main(int argc, char *argv[]){
    char *msg = "this is test...\n";
        //open a file with write mode, if the file is not exists, create a new one
        //if exists, trunc it
        int flags = O_WRONLY|O_CREAT|O_TRUNC;
        int fd = open(argv[1], flags, 0644);
        if(fd == -1) return -1;
        printf("%d\n", fd);
        int s_fd = dup(1); //redirect standard output 1 to s_fd = 4
        printf("%d\n", s_fd);
        dup2(fd, 1); //copy the opened file descriptor to standard output
        close(fd);
        //write to a file by standard output descriptor
        write(1, msg, strlen(msg));
        //recovre standard output to screen
        dup2(s_fd, 1);
        close(s_fd);
        write(1, msg, strlen(msg));

        return 0;
}

输出:

zhongjun@eclipse:~$ gcc direct.c -o out
zhongjun@eclipse:~$ ./out ts
3
4
this is test...

相关文章

  • 8.文件管理

    1.cat命令 copy命令的实现输入文件通过argv[1],输出保存到argv[2]。若目标文件不存在,创建一个...

  • Linux常用指令

    linux常用指令: 1.文件管理2.目录管理3.用户管理4.权限管理5.文件搜索6.内容搜索7.压缩包管理8.网...

  • 8.文件操作

    // 命令行参数 // 命令行参数用于向程序传递在执行阶段才确定的数据 // 提升软件的可移植行 int main...

  • 8.文件上传

    云服务器安装的系统一般为Linux,经常会遇到将文件从window系统上传到Linux系统这种情况,记录一下: 将...

  • 【Python爬虫】- 8.第八次 Python文件操作

    # 8.第八次 Python文件操作 # 一、创建txt文件,写入文本如下: # name,sex # '攀攀',...

  • NSFileManager 文件管理

    文件管理 - 创建 文件管理 -- 添加 文件管理 --- 删除 文件管理 --- 复制

  • K12D9-74/100|《当今饭店业》

    8.俱乐部管理 概要 俱乐部产生的背景 俱乐部的类型 城市俱乐部 乡村俱乐部 其他俱乐部 俱乐部...

  • 01-必备计算机知识

    一、文件管理 文件分类 良好的文件分类管理,方便于查找和管理文件。 文件命名 文件后缀文件按照不同的格式和用途分很...

  • 01-必备计算机知识

    一、文件管理 文件分类 良好的文件分类管理,方便于查找和管理文件。 文件命名 文件后缀文件按照不同的格式和用途分很...

  • 操作系统(4) -- 文件管理、IO管理

    内容大纲 1、文件管理; 2、文件系统管理 3、输入/输出管理 4、缓冲区 1 文件管理 文件 文件是指记录在外存...

网友评论

      本文标题:8.文件管理

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