简体   繁体   English

WinApi与文本文件

[英]WinApi with text files

I need to copy all files from 1 directory to another, but it's not working. 我需要将所有文件从一个目录复制到另一个目录,但是它不起作用。

   #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <string>

using namespace std;

int main(int argc, char *argv[])
{
    TCHAR buffer[256];
    SetCurrentDirectory ("C:\\Users\\Rinat\\Desktop\\SP\\1");
    GetCurrentDirectory (sizeof (buffer), buffer);
    printf ("%s\n", buffer);
    WIN32_FIND_DATA FindData;
    HANDLE MyFile;

    MyFile = FindFirstFile ("*", &FindData);
    if (MyFile != INVALID_HANDLE_VALUE) {
    do {
    printf ("%s\n", FindData.cFileName);

         CopyFile("C:\\Users\\Rinat\\Desktop\\SP\\1"+FindData.cFileName, "C:\\Users\\Rinat\\Desktop\\SP\\2\\" + FindData.cFileName, FALSE);

      } while (FindNextFile (MyFile, &FindData));
   FindClose (MyFile);
}

    system("PAUSE");
    return EXIT_SUCCESS;
}

The error is 22 C:\\Users\\Rinat\\Desktop\\SP\\7.cpp invalid operands of types const char[28] and CHAR[260] to binary operator+ 错误为二进制operator+ 22 C:\\ Users \\ Rinat \\ Desktop \\ SP \\ 7.cpp类型为const char[28]CHAR[260]类型的无效操作数

You can't add string pointers together like that in C++. 您不能像在C ++中那样将字符串指针加在一起。 You need to use a function (or a class like std::string ). 您需要使用一个函数(或类似std::string类的类)。

do {
    char chSrc[MAX_PATH], cdDst[MAX_PATH];
    StringCchCopy(chSrc, MAX_PATH, "C:\\Users\\Rinat\\Desktop\\SP\\1\\");
    StringCchCat(chSrc, MAX_PATH, FindData.cFileName);
    StringCchCopy(chDst, MAX_PATH, "C:\\Users\\Rinat\\Desktop\\SP\\2\\");
    StringCchCat(chDst, MAX_PATH, FindData.cFileName);
    CopyFile(chSrc, chDst, TRUE);
} ...

Using std::string : 使用std::string

do {
    CopyFile((std::string("C:\\Users\\Rinat\\Desktop\\SP\\1\\") + FindData.cFileName)).c_str(),
             (std::string("C:\\Users\\Rinat\\Desktop\\SP\\2\\") + FindData.cFileName)).c_str(),
             TRUE);
} ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM