0

I have a text file with the names of about 800 files I want to transfer from one folder to another. Basically, the text file looks like this :

file1.aaa (End of line)
file2.aaa
..
etc

I made this code, using the function 'rename' as everyone suggests on the internet :

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( void )
{
    FILE *file = fopen ( "C:\\Users\\blabla\\ListOfFiles.txt", "r" );
    char path1[100] = "C:\\blabla\\folder1\\";
    char path2[100] = "C:\\blabla\\folder2\\";
    char *s1;
    char *s2;

    char line [20]; /* the file names won't be any longer than that */
    while(fgets(line, sizeof line,file) != NULL)
    {
        char *filePath1 = (char *) malloc((strlen(path1) + strlen(line) + 1) * sizeof(char));
        char *filePath2 = (char *) malloc((strlen(path2) + strlen(line) + 1) * sizeof(char));
        filePath1 = strcpy(filePath1, path1);
        filePath2 = strcpy(filePath2, path2);
        strcat(filePath1,line);
        strcat(filePath2,line);


       if (rename(filePath1, filePath2) != 0)
       {
           perror("wrong renaming");
           getchar();
       }

       free(filePath1);
       free(filePath2);

    }

    fclose (file);

    return 0;
}

Now, when I print the filepaths I get the expected results, but the program stops running when it's supposed to run the 'rename' function, because of an invalid argument problem. I looked at http://www.cplusplus.com/ and noticed that it says the arguments of rename() should be const char*, could this be where the problem come from ? But if so, I don't see how I could turn my arguments into 'const', since I need to update them as I read my initial text file.

dragosht
  • 3,237
  • 2
  • 23
  • 32
Nicolas
  • 11
  • 3
  • 1
    Are you solving a general problem, or do you really just want to copy the set of files once? Your operating system will have *far superior* tools for that. – Kerrek SB Jul 07 '14 at 08:44
  • Do want to write C or C++ code? – TWE Jul 07 '14 at 08:45
  • 1
    Use your OS's shell. You will get this done in ten minutes. This should help: [Using the FOR command to copy files listed in a text file](http://www.sidesofmarch.com/index.php/archive/2004/03/30/using-the-for-command-to-copy-files-listed-in-a-text-file/) – sampathsris Jul 07 '14 at 08:51
  • The target folder exists? Is it empty? If not, can you overwrite? – laune Jul 07 '14 at 08:52
  • What is exactly the error message you get? – Emanuele Paolini Jul 07 '14 at 08:56
  • 1)Yes this is part of a more general thing 2 & 3) C, I changed that 4) Yes it does 5) "wrong renaming" : Invalid Argument – Nicolas Jul 07 '14 at 08:56
  • 2
    `fgets() reains the '\n' in the read string; you'll have to strip that before using the "filename" – joop Jul 07 '14 at 09:02
  • Other way to use system call and put standard cp or mv calls to move file from one directory to another. – Abhinav Jul 07 '14 at 09:02
  • I still don't know why my code doesn't work, but I've found a way thanks to Krumia's link to temporarily fix my problems. Thanks! – Nicolas Jul 07 '14 at 09:10

2 Answers2

0

The code that builds the file paths is horribly over-complicated but should work. To simplify it, remove the malloc() and just use two statically-sized arrays. Also, for the future, please don't cast the return value of malloc() in C.

You're misunderstanding the const thing, it means that rename() won't change the characters pointed at by its two arguments. It's a way to say "these two pointers point at data which is input-only to this function, there will be no attempt to modify that data from inside the function". You should always const argument pointers when possible, it helps make the code much clearer.

If you're getting "invalid argument", that probably means the files aren't being found. Print out the filenames to help you verify.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thanks for the clarification on how const things work. The filepaths are correct as far as I can tell by printing them – Nicolas Jul 07 '14 at 08:58
0

I suggest you to take a look at:

How can I copy a file on Unix using C?

And replace "/bin/cp" for "/bin/mv" in that code.

Hope it helps!

Community
  • 1
  • 1
Pablo Stark
  • 682
  • 10
  • 34