-2

Here is an example my code to copy file from src to dst:

int main() {

    const wchar_t* file[3] = { L"\\abc.txt", L"\\def.txt", L"\\ghi.txt" };

    for (int i = 0; i < 3; i++) {
        wchar_t* src = funcA();
        wchar_t* dst = funcB();
        wcscat(dst, file[i]);
        wcscat(src, file[i]);
        CopyFileW(src, dst, TRUE);
    }
}

Is there a better way to do that? Can I optimize the code by not running the funcA and funcB many times?

Biswapriyo
  • 3,491
  • 3
  • 22
  • 42
  • 1
    Can you explain in more details what you are trying to achieve? Also can you add an [MVCE](https://stackoverflow.com/help/mcve) – ctrl-shift-esc Mar 28 '18 at 10:02
  • Yes, you need to write a loop. Did you try that? If yes, please tell us what the exact problem is? Not sure if you need to use `malloc`. The question is quite unclear. We need more information. Please read this: [ask] – Jabberwocky Mar 28 '18 at 10:08
  • 1
    If you put your loop it would be clearer @Biswapriyo. For now we are trying to guess. – Novy Mar 28 '18 at 10:13
  • 1
    `wchar_t name[3]`: `name` is not an array of three file names but an array of three chars. It seems you should learn some basics of C before proceeding with your program – Jabberwocky Mar 28 '18 at 10:14
  • 1
    If you want to copy multiple files, use the [IFileOperation](https://msdn.microsoft.com/en-us/library/windows/desktop/bb775771.aspx) interface. To use the interface successfully, you need to stop trying to learn programming by guessing at what your code does. – IInspectable Mar 28 '18 at 10:20
  • Your example invokes undefined behaviour. Your array only provide enough memory to hold the folder. Adding any further characters using `wscat_s` is accessing memory out of bounds. You should specify a length for your array definition. – Gerhardh Mar 28 '18 at 10:48
  • 1
    You problem is not that you aren't able to copy multiple files, it's that you don't let know how to even represent multiple file names in a data structure. – David Heffernan Mar 28 '18 at 10:48
  • Generally, VLA are faster than heap allocation. What's most suitable here is impossible to tell, there's not enough context. – Lundin Mar 28 '18 at 11:02
  • 1
    @Biswapriyo - Stack or heap allocation takes zero time compared to copying a file on disk. So don't bother optimizing the fast part. – Bo Persson Mar 28 '18 at 11:10
  • Great, even more undefined behavior! Now you are writing outside array bounds of read-only arrays. You really need to stop guessing. Get a few introductory books on C, and take it from there. Tutorials, or - worse - online video courses, won't help you in building a solid foundation. This is what you need. – IInspectable Mar 28 '18 at 12:18

1 Answers1

1

After you fix the errors, your code is copying the file to itself. It boils down to this:

const wchar_t* file[3] = { L"\\abc.txt", L"\\def.txt", L"\\ghi.txt" };
for(int i = 0; i < _countof(file); i++)
    CopyFile(file[i], file[i], TRUE);

It's just copying "\\abc.txt" to "\\abc.txt", and "\\def.txt" to "\\def.txt", etc.

Presumably your intention is to copy multiple files from one directory to another directory. For example copy "abc.txt" to "c:\\target\\abc.txt", etc.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void)
{
    const wchar_t* files[3] = { L"1.txt", L"2.txt", L"3.txt" };

    wchar_t *directory = L"c:\\target";
    wchar_t dst[MAX_PATH] = { 0 };
    for(int i = 0; i < _countof(files); i++)
    {
        swprintf(dst, _countof(dst), L"%ls\\%ls", directory, files[i]);
        CopyFileW(files[i], dst, TRUE);
    }

    return 0;
}

Note that I changed the source to "abc.txt" and changed the format specifier in swprintf

In a practical application you may want to copy "c:\\src\\1.txt" to "c:\\dst\\1.txt", like this code:

#define UNICODE
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <Shlwapi.h>

#pragma comment(lib, "Shlwapi.lib")//Add library (Visual Studio specific)

int main(void)
{
    const wchar_t* files[3] =
        { L"c:\\src\\1.txt", L"c:\\src\\2.txt", L"c:\\src\\3.txt" };

    wchar_t *directory = L"c:\\dst";
    wchar_t dst[MAX_PATH] = { 0 };

    if(!PathFileExists(directory))
    {
        wprintf(L"path error\n");
        return 0;
    }

    for(int i = 0; i < _countof(files); i++)
    {
        swprintf(dst, _countof(dst), L"%ls\\%ls", directory, PathFindFileName(files[i]));
        //CopyFile(files[i], dst, TRUE);
        wprintf(L"copy %s to %s\n", files[i], dst);
    }

    return 0;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77