• 大小: 0M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: 其他
  • 标签: 其他  

资源简介

实验五.zip

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//整体思路同windows非常相似
void Mycp(char *fsourcechar *ftarget);       //将源目录信息复制到目标目录下
void CopyFile(char *fsourcechar *ftarget);  //直接复制
//各种函数原型的应用,参数的设置,查找很多函数资料
int main(int argcchar *argv[])
{
    struct stat statbuf;     //stat结构
    struct utimbuf timeby;  //文件时间结构
    /*
    文件相关三个时间字段stat
    st_atime    最后存取时间
    st_mtime    最后修改时间
    st_ctime    i节点状态的最后更改时间
    struct utimbuf
    {
        time_t actime;         文件数据的最后存取时间
        time_t modtime;        文件数据的最后修改时间
    }
    */
    DIR * dir;             //DIR结构的指针,指向目录的第一个文件
    if(argc != 3)          //参数出错
    {
        printf(“ARGC ERROR!\N“);
    }
    else
    {
        /*opendirclosedir
        DIR * opendir(const char *name);
        int closedir(DIR * dir);    关闭指定目录文件,释放相关资源
        */
        if((dir = opendir(argv[1])) == NULL)
        {
            printf(“Source Folder does not exist.\n“);    //源文件打开出错
        }
        if((dir = opendir(argv[2])) == NULL)
        {
            //时间属性
            /*utime函数:修改文件的存取和修改时间
            int utime(const char *filenameconst struct utimbuf buf);
            */
            //相当于windows中CreateFileD函数功能
            stat(argv[1]&statbuf);
            /*
            stat(const char *file_namestruct stat *buf)
            统计文件名指定的文件属性信息
            */
            mkdir(argv[2]statbuf.st_mode);    //创建目录
            /*
            mkdir(const char * dir_pathnamemode_t mode)
            rmdir(const char * dir_pathname)  删除
            */
            timeby.actime = statbuf.st_atime;   //修改时间属性,存取时间
            timeby.modtime = statbuf.st_mtime;  //修改时间
            utime(argv[2]&timeby);
        }
        Mycp(argv[1]argv[2]);    //开始复制
    }
    printf(“Copy Finished!\n“);
    return 0;
}

void Mycp(char *fsourcechar *ftarget)
{
   char source[512];
   char target[512];
   struct stat statbuf;
   struct utimbuf timeby;
   DIR *dir;
   struct dirent * entry;
   /*
   struct dirent
   {ino_t d_ino;                  inode索引节点号
    char d_name[NAME_MAX+1]       文件名
    unsigned char d_type;         文件类型
   }
   */
   strcpy(sourcefsource);
   strcpy(targetftarget);
   dir = opendir(source);     //打开目录返回指向DIR结构的指针
   //refer to P215
   while((entry = readdir(dir)) != NULL)  //读目录
   /*
   readdir
   struct dirent *readdir(DIR *dir)
   */
   {
       if(strcmp(entry->d_name“.“) == 0 || strcmp(entry->d_name“..“) == 0)   //判断目录
                continue;
       if(entry->d_type == 4)
       {
           strcat(source“/“);
           strcat(sourceentry->d_name);
           strcat(target“/“);
           strcat(targetentry->d_name);
           //相当于windows中CreateFileD函数功能
           stat(source&statbuf);   //统计文件属性信息
           mkdir(targetstatbuf.st

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-04-26 15:57  实验五\linux\
     文件        5031  2014-04-24 15:39  实验五\linux\LINUXCP.c
     目录           0  2015-04-26 15:57  实验五\windows\
     文件        7581  2014-04-24 15:01  实验五\windows\winmycp.cpp
     目录           0  2015-04-26 15:57  实验五\

评论

共有 条评论