Copy files in c

Asked

Viewed 675 times

1

In C language, how do I copy the file gg.bat for "%AppData%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

I tried with

system("copy \"gg.bat\" \"%%appdata%%\\microsoft\\windows\\start menu\\programs\\startup\\\"");

But it is appearing:

The file name, directory name or label syntax volume is incorrect. 0 file(s) copied(s).

  • Don’t forget that the "" character is used as an escape prefix for several letters within a string (for example " n" turns into the Newline character code 10) - so always use two bars " in the file name.

  • Other: Voce knows that with Sytem("copy") you are not "copying the file' in C, is not? The copy runs an external process that copies. A C copy would involve opening the original file for reading, the target file for writing, and reading the contents of one and writing to the other. But the final practical effect of the system("copy") is the same

1 answer

1

Try this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

int main(int argc, char *argv[]){
    // aloca um texto para o system()
    char *command = malloc(
            strlen(argv[1])+ // tamanho da primeira string
            strlen(argv[2])+ // tamanho da segunda string
            32); // espaço extra

    sprintf(command, "cp \"%s\" \"%s\"", argv[1], argv[2]);

    system(command);

    return 1;
}

to call just run the executable with 2 parameters: <exe> <arquivo1> <arquivo2>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.