0

I'm quite new to C and I'm having some trouble with a program to transfer files from one hard drive to another. The PC the program will be running from doesn't have any kind of compiler, so I want to build in the aility for the source and destination paths to change without having to re-compile the program. As it currently stands, in the current directory there are two text files - one with the source path, the other with the destination. The program reads the lines from these files and uses these respective paths for the transfer.

To simplify things from my end, to keep the program running continuously (as this is required) I have set a 1 second loop as opposed to using system threading.

I think the problem lies with using string variables as the directory paths in the system command - because if I hard code paths in this command the transfer works successfully. In the current arrangement, I get the error "The file name, directory name, or volume label syntax is incorrect." in my program. Does anybody have any suggestions? Should I be using sprintf to convert a line from a text file into a string?

#include <stdio.h>
#include <time.h>
#include <string.h>

void delay(int seconds);

int main()
{
int x=1;
chdir("C:\\Users\\jw\\Documents\\");
FILE *file_src;
FILE *file_dst;
file_src=fopen("source_dir.txt","r");
file_dst=fopen("dest_dir.txt","r");

char message[150][150],buffer[150];
char* source_directory;
char* destination_directory;

fgets(buffer,150,file_src);
strcpy(message[1],buffer);
sprintf(data,"%s",message[1]);
source_directory=message[1];

fgets(buffer,150,file_dst);
strcpy(message[2],buffer);
sprintf(data2,"%s",message[2]);
destination_directory=message[2];


printf("source folder: %s \n",message[1]);
printf("destination folder: %s \n",message[2]);
for(x=1;x=1;x=1)
{
    system("move *%s *%s",source_directory,destination_directory);
    delay(1);
}

printf("/n")

return(0);
}

void delay(int seconds)
{
long pause;
clock_t now,then;

pause = seconds*(CLOCKS_PER_SEC);
now = then = clock();
while( (now-then) < pause )
    now = clock();
}
  • 1
    Did you know that arrays are indexed from `0` to `N-1`? – Iharob Al Asimi Jan 26 '15 at 11:05
  • 1
    If you are writing a program to move files, what is the point in using `system()` for that matter write a script and avoid the trouble. – Iharob Al Asimi Jan 26 '15 at 11:06
  • 1
    What is `data` and `data2`? Also, use `for(;;)` if you want an infinite loop. And `message[0]` is unused. `/n`!=newline. Newline==`\n` – Spikatrix Jan 26 '15 at 11:14
  • 1
    Why do you use `fgets` to read into a buffer, the use `strcpy` to move to another buffer, then `sprintf` to put the string in yet a third buffer? Why not just read it wherever you want it in the first place? – David Schwartz Jan 26 '15 at 11:15
  • Numerous problems with the code, but in particular you have absolutely no error checking (at the very minimum you should check for failure after calls to `fopen`) and there are some very obvious coding errors, e.g. what do you expect `for(x=1;x=1;x=1)` to do ? – Paul R Jan 26 '15 at 11:16
  • 1
    Honestly, about the best possible answer to this question would be, "You have to learn C if you want to write C code." I can't even imagine what thought process could lead a person to write `for(x=1;x=1;x=1)`. – David Schwartz Jan 26 '15 at 11:18

1 Answers1

1

fgets keeps the newline character, so your string becomes:

move *source\n *dest\n

You need to remove trailing \n characters from the input.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82