c++ - 带有文本文件的 WinApi

标签 c++ winapi text

我需要将所有文件从一个目录复制到另一个目录,但它不起作用。

   #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;
}

错误是 22 C:\Users\Rinat\Desktop\SP\7.cpp invalid operands of types const char[28] and CHAR[260] to二进制 operator+

最佳答案

您不能像在 C++ 中那样将字符串指针加在一起。您需要使用一个函数(或一个类似 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);
} ...

使用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);
} ...

关于c++ - 带有文本文件的 WinApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134684/

相关文章:

c++ - 在 C++ 项目中使用基于 CUDA 的库代码

c++ - 如何确定网络连接正在使用哪个端口?

python - 将控制台输入行保持在输出下方

C++ getIntArrayAverage 函数

c++ - 频繁读/写小文件会降低性能吗?

c++ - C++ 中的 MSD 基数排序(字典顺序)

perl Win32::SerialPort:读取和输入方法之间的区别

c - Win32 : How to create a ListBox control using the CreateWindowExW() function?

java - JLabel 到 GridBagLayout。如何固定宽度?

android - 如何在 Android 的纯文本文件中执行查询?