zoukankan      html  css  js  c++  java
  • Linux 目录操作和4中文件拷贝效率测试

    /*
    1、用户输入任意目录名称,显示该目录下的文件列表信息,包括文件类型,文件权限,文件大小,文件名称
    2、拷贝用户输入的文件到当前目录下
    3、第二点功能,使用4种方式完成,并比较说明效率
    */

    /*

    struct stat {
    dev_t st_dev; //文件的设备编号
    ino_t st_ino; //节点
    mode_t st_mode; //文件的类型和存取的权限
    nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
    uid_t st_uid; //用户ID
    gid_t st_gid; //组ID
    dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
    off_t st_size; //文件字节数(文件大小)
    unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
    unsigned long st_blocks; //块数
    time_t st_atime; //最后一次访问时间
    time_t st_mtime; //最后一次修改时间
    time_t st_ctime; //最后一次改变时间(指属性)
    };

    先前所描述的st_mode 则定义了下列数种情况:
    S_IFMT 0170000 文件类型的位遮罩
    S_IFSOCK 0140000 scoket
    S_IFLNK 0120000 符号连接
    S_IFREG 0100000 一般文件
    S_IFBLK 0060000 区块装置
    S_IFDIR 0040000 目录
    S_IFCHR 0020000 字符装置
    S_IFIFO 0010000 先进先出

    S_ISUID 04000 文件的(set user-id on execution)位
    S_ISGID 02000 文件的(set group-id on execution)位
    S_ISVTX 01000 文件的sticky位

    S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限
    S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限
    S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限

    S_IRGRP 00040 用户组具可读取权限
    S_IWGRP 00020 用户组具可写入权限
    S_IXGRP 00010 用户组具可执行权限

    S_IROTH 00004 其他用户具可读取权限
    S_IWOTH 00002 其他用户具可写入权限
    S_IXOTH 00001 其他用户具可执行权限

    上述的文件类型在POSIX中定义了检查这些类型的宏定义:
    S_ISLNK (st_mode) 判断是否为符号连接
    S_ISREG (st_mode) 是否为一般文件
    S_ISDIR (st_mode) 是否为目录
    S_ISCHR (st_mode) 是否为字符装置文件
    S_ISBLK (s3e) 是否为先进先出
    S_ISSOCK (st_mode) 是否为socket
    */

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

    int mode_to_letters(int mode,char *str)
    {


    strcpy(str,"----------");

    if(S_ISDIR(mode))str[0] = 'd';

    if(S_ISCHR(mode))str[0] = 'c';

    if(S_ISBLK(mode))str[0] = 'b';


    if(mode&S_IRUSR)str[1] = 'r';

    if(mode&S_IWUSR)str[2] = 'w';

    if(mode&S_IXUSR)str[3] = 'x';

    if(mode&S_IRGRP)str[4] = 'r';

    if(mode&S_IWGRP)str[5] = 'w';

    if(mode&S_IXGRP)str[6] = 'x';

    if(mode&S_IROTH)str[7] = 'r';

    if(mode&S_IWOTH)str[8] = 'w';

    if(mode&S_IXOTH)str[9] = 'x';

    return 0;

    }
    int dostat(const char *path)
    {
    struct stat info;
    char modestr[16];
    if(stat(path, &info)==-1)
    {
    printf("%s stat error ",path);
    return -1;

    }
    printf(" %s %ld ", path,info.st_size);
    mode_to_letters(info.st_mode,modestr);

    printf("%s ",modestr);
    return 0;
    }




    int cpchar(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char ch;

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src fail ");
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des fail ");
    return -1;
    }

    while(1)
    {
    if((ch=fgetc(fpsrc))!=EOF)
    {
    fputc(ch,fpdes);
    //printf("%c",ch);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

    /*fgets fputs 一行*/
    int cpline(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char buf[1024];

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src fail ");
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des fail ");
    return -1;
    }

    while(1)
    {
    if(fgets(buf,1024,fpsrc)!=NULL)
    {
    fputs(buf,fpdes);
    //printf("%s",buf);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

    /*fread fwrite 一块*/
    int cpblk(char *src, char *des)
    {
    FILE *fpsrc,*fpdes;
    char buf[1024];
    int num;

    fpsrc = fopen(src, "r");
    if(fpsrc==NULL)
    {
    printf("open src %s fail ",src);
    return -1;
    }

    fpdes = fopen(des, "w");
    if(fpdes==NULL)
    {
    printf("open des %s fail ",des);
    return -1;
    }

    while(1)
    {
    if((num=fread(buf,sizeof(char),1024,fpsrc))>0)
    {
    fwrite(buf,sizeof(char),num,fpdes);
    //printf("%d ",num);
    }
    else
    {
    break;
    }
    }

    fclose(fpsrc);
    fclose(fpdes);

    return 0;
    }

    /*系统read write*/
    int cp(char *src, char *des)
    {
    int fd1,fd2;
    char buf[1024];
    int nbyte;

    if((fd1 = open(src, O_RDONLY)) < 0)
    {
    printf("open src fail! ");
    return -1;
    }


    if((fd2 = open(des, O_WRONLY)) < 0)
    {
    printf("open des fail! ");
    return -1;
    }

    while((nbyte = read(fd1, buf, sizeof(buf))) > 0)
    {
    write(fd2, buf, nbyte);
    }

    close(fd1);
    close(fd2);

    return 0;
    }

    int mydir(const char *dirname){
    DIR *dp;
    struct dirent *dir;
    dp=opendir(dirname);
    char path[256];
    char inputfilename[256];
    char pathbuf[256];
    char path_getcwd[256];
    int ret;
    if(dp==NULL)
    {

    printf("open dir fail");
    return -1;
    }
    strncpy(path,dirname,strlen(dirname)+1);
    while(1)
    {
    dir=readdir(dp);
    if(dir!=NULL)
    {
    //printf("%ld %s ",dir->d_ino,dir->d_name);

    strncpy(pathbuf,path,strlen(path)+1);
    dostat(strcat(strcat(pathbuf,"/"),dir->d_name));

    }
    else
    {
    break;

    }
    }
    //拷贝用户输入的文件到当前目录下
    printf("please input copy file name to current dir ");
    scanf("%s",inputfilename);
    strncpy(pathbuf,path,strlen(path)+1);
    printf("%s ",pathbuf);
    getcwd(path_getcwd,256);


    strcat(strcat(pathbuf,"/"),inputfilename);
    strcat(strcat(path_getcwd,"/"),inputfilename);
    struct timeval tv;
    long start_time,stop_time,use_time;

    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    start_time=tv.tv_sec*1000+tv.tv_usec/1000;

    ret=cp(pathbuf,path_getcwd);

    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    stop_time=tv.tv_sec*1000+tv.tv_usec/1000;
    use_time=stop_time-start_time;
    if(ret==0)
    {

    printf("cp test success! use time= %ld msec ",use_time);
    }


    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    start_time=tv.tv_sec*1000+tv.tv_usec/1000;

    ret=cpchar(pathbuf,path_getcwd);

    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    stop_time=tv.tv_sec*1000+tv.tv_usec/1000;
    use_time=stop_time-start_time;
    if(ret==0)
    {

    printf("cpchar test success! use time= %ld msec ",use_time);
    }


    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    start_time=tv.tv_sec*1000+tv.tv_usec/1000;

    ret=cpline(pathbuf,path_getcwd);

    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    stop_time=tv.tv_sec*1000+tv.tv_usec/1000;
    use_time=stop_time-start_time;
    if(ret==0)
    {

    printf("cpline test success! use time= %ld msec ",use_time);
    }


    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    start_time=tv.tv_sec*1000+tv.tv_usec/1000;

    ret=cpblk(pathbuf,path_getcwd);

    gettimeofday(&tv,NULL);
    //printf("tv_usec:%d ",tv.tv_usec);
    stop_time=tv.tv_sec*1000+tv.tv_usec/1000;
    use_time=stop_time-start_time;
    if(ret==0)
    {

    printf("cpblk test success! use time= %ld msec ",use_time);
    }



    closedir(dp);



    return 0;
    }


    int main(int argc, char * argv[])
    {
    if(argc != 2)
    {
    printf("Input dir error ");
    return -1;
    }

    mydir(argv[1]);
    return 0;
    }

    运行结果如下,read write 最快, fread fwrite次之, 之后是fputs 和fputc.

    另外fputs和fputc用于非字符串文件时会有问题,深层原因还待分析。

     

  • 相关阅读:
    力扣3. 无重复字符的最长子串
    力扣724. 寻找数组的中心索引
    力扣105. 从前序与中序遍历序列构造二叉树
    力扣541. 反转字符串 II
    力扣496. 下一个更大元素 I
    力扣129. 求根到叶子节点数字之和
    力扣628. 三个数的最大乘积
    力扣415. 字符串相加
    力扣409. 最长回文串
    力扣404. 左叶子之和
  • 原文地址:https://www.cnblogs.com/chenglongxu/p/5295400.html
Copyright © 2011-2022 走看看