簡體   English   中英

使用C的文件傳輸程序

[英]File transfer program using C

我對C很陌生,並且在將文件從一個硬盤驅動器傳輸到另一個硬盤的程序時遇到了一些麻煩。 程序將在其上運行的PC沒有任何類型的編譯器,因此我想構建一種功能來更改源路徑和目標路徑,而不必重新編譯該程序。 按照目前的狀態,在當前目錄中有兩個文本文件-一個帶有源路徑,另一個帶有目標。 該程序從這些文件中讀取行,並使用這些相應的路徑進行傳輸。

為了簡化工作,為了使程序連續運行(這是必需的),我設置了一個1秒的循環,而不是使用系統線程。

我認為問題在於將字符串變量用作系統命令中的目錄路徑-因為如果我在此命令中使用硬編碼路徑,則傳輸將成功進行。 在當前的安排中,出現錯誤“文件名,目錄名或卷標簽語法不正確”。 在我的程序中。 有人有什么建議嗎? 我應該使用sprintf將文本文件中的一行轉換為字符串嗎?

#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保留換行符,因此您的字符串變為:

move *source\n *dest\n

您需要從輸入中刪除尾隨\\n字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM