简体   繁体   中英

File transfer program using C

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();
}

fgets keeps the newline character, so your string becomes:

move *source\n *dest\n

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM