gpt4 book ai didi

c - 使用C语言的文件传输程序

转载 作者:行者123 更新时间:2023-11-30 15:24:56 27 4
gpt4 key购买 nike

我对 C 语言很陌生,并且在使用将文件从一个硬盘驱动器传输到另一个硬盘驱动器的程序时遇到了一些问题。运行程序的电脑没有任何类型的编译器,因此我想构建更改源路径和目标路径的能力,而无需重新编译程序。就目前而言,当前目录中有两个文本文件 - 一个包含源路径,另一个包含目标路径。程序从这些文件中读取行并使用这些各自的路径进行传输。

为了从我的角度简化事情,保持程序连续运行(因为这是必需的),我设置了 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 字符。

关于c - 使用C语言的文件传输程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28148837/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com