zoukankan      html  css  js  c++  java
  • C++笔记之CopyFile和MoveFile的使用

    1、函数定义

    CopyFile(A, B, FALSE);表示将文件A拷贝到B,如果B已经存在则覆盖(第三参数为TRUE时表示不覆盖)

    MoveFile(A, B);表示将文件A移动到B

    2.函数原型

    CopyFile:

    MoveFile:

    由函数原型可以看出,这两个函数的前两个输入参数都为LRCWSTR类型,如果我们定义的是char*,记得转换成LRCWSTR,否则会报错;

    另外,这两个函数都返回一个bool型变量,表示执行成功与否,当目标位置路径不存在时,会return 0

     3、Demo

    示例一:

    CopyFile:

    #include <fstream>
    #include <windows.h>
     
    int main()
    {
        char *fn = "test.txt";
     
        std::ofstream out(fn);
        if (!out.is_open())
            return 0;
        out.close();
     
        WCHAR buf[256];
        memset(buf, 0, sizeof(buf));
        MultiByteToWideChar(CP_ACP, 0, fn, strlen(fn) + 1, buf, sizeof(buf) / sizeof(buf[0]));
        CopyFile(buf, L"../file/output.txt", FALSE);//FALSE:如果目标位置已经存在同名文件,就覆盖,return 1
                                                    //TRUE:如果目标位置已经存在同名文件,则补拷贝,return 0
                                                    //后者路径若不错在,return 0
        system("pause");
        return 1;
    }

    CopyFile:

    #include <fstream>
    #include <windows.h>
     
    int main()
    {
        char *fn = "test.txt";
     
        std::ofstream out(fn);
        if (!out.is_open())
            return 0;
        out.close();
     
        WCHAR buf[256];
        memset(buf, 0, sizeof(buf));
        MultiByteToWideChar(CP_ACP, 0, fn, strlen(fn) + 1, buf, sizeof(buf) / sizeof(buf[0]));
        MoveFile(buf, L"../file/output.txt");//FALSE:将前者移动到后者中(后者路径若不错在,return 0)
     
        system("pause");
        return 1;
    }

    示例二:

    #include <WINDOWS.H>
     
    int main()
    {
        char *sourcefile = "d://source//p.png";//源文件
        char *targetfile = "d://target//q.png";//目标文件
        CopyFile(sourcefile , targetfile , FALSE);//false代表覆盖,true不覆盖
        return 0;
    }

    4、将图片批量复制到另一个文件夹

    //MyCopyFile.cpp

    #include <iostream> #include <stdio.h> #include <opencv2/opencv.hpp> #include "io.h" #include <fstream> #include <WINDOWS.H> //define the buffer size. Do not change the size! #define DETECT_BUFFER_SIZE 0x20000 using namespace std; //getFiles_Name函数声明,作用:读取path路径下的.png格式文件,并将每个.png文件的路径和文件名分别存储到files和filesname void getFiles_Name(string path, vector<string>& files, vector<string>& filesname); int main(void) { vector<string> classnames; classnames.push_back(string("disgust")); classnames.push_back(string("neutral")); classnames.push_back(string("scream")); classnames.push_back(string("smile")); classnames.push_back(string("squint")); classnames.push_back(string("surprise")); for (int iexpress = 0; iexpress < 7;iexpress++) { string inputStr = "C:\SourceFile\" + classnames[iexpress]; string outputStr = "C:\TargetFile\" + classnames[iexpress] + "\"; vector<string> files; vector<string> filesname; ////获取该路径下的所有文件 getFiles_Name(inputStr, files, filesname); //循环复制文件 for (int k = 0; k < files.size(); k++) { unsigned char *pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer) { fprintf(stderr, "Can not alloc buffer. "); return -1; } cout << files[k] << endl; CopyFile(files[k].c_str(), (outputStr + filesname[k]).c_str(), FALSE);//false代表覆盖,true不覆盖      //若文件路径为string类型变量,例如为pathstr,则需使用pathstr.c_str()转换即可; free(pBuffer); } } return 0; } void getFiles_Name(string path, vector<string>& files, vector<string>& filesname) { //文件句柄 intptr_t hFile; //文件信息,声明一个存储文件信息的结构体 struct _finddata_t fileinfo; string p;//字符串,存放路径 //string name; if ((hFile = _findfirst(p.assign(path).append("\*.png").c_str(), &fileinfo)) != -1)//若查找成功,则进入 { do { files.push_back(path + "\" + fileinfo.name); filesname.push_back(fileinfo.name); } while (_findnext(hFile, &fileinfo) == 0); //_findclose函数结束查找 _findclose(hFile); } }

    如果出现以下错误:

    不能从const char*转换为LPCWSTR的原因及解决方法:

    解决方法:

    项目-->2.MyCopyFile属性-->3.配置属性-->4.常规-->5.字符集:改成 未设置

    错误原因:

    因为我的程序在UNICODE(宽字节)字符集下运行, UNICODE与ANSI有什么区别呢?简单的说,UNICODE版的字符比ANSI 的内存占用大,比如:Win32程式中出现的标准定义 char 占一个字节,而 char 的UNICODE版被定义成这样: typedef unsigned short wchar_t ;占2个字节。 所以有字符做参数的函数相应也用两个版本了。

    参考博客:

    https://blog.csdn.net/u012043391/article/details/77663644

    https://blog.csdn.net/callmeado/article/details/21826679 

    https://www.cnblogs.com/dongsheng/p/3586418.html

    https://blog.csdn.net/linjingtu/article/details/53190491

  • 相关阅读:
    横竖屏判断及禁止浏览器滑动条
    sinablog to cnblogs
    qml 3d 纪念那些曾经爬过的坑
    opencv 学习一安装环境vs2015+opencv3
    vs2015 调试 无法启动程序
    qtableview 表格风格设置
    qt 给父窗体设置样式不影响子控件样式以及子控件设置透明
    Qt ASSERT:"QMetaObjectPrivate::get(smeta)->revision>= 7"in file kernelqobject.cpp,line 2646
    cmake 学习-cmakelists.txt
    CMake学习- 使用批处理设置编译环境
  • 原文地址:https://www.cnblogs.com/yuehouse/p/10159027.html
Copyright © 2011-2022 走看看