如何用c语言将文件复制到自己想要的文件夹??

如何在此下,补充?以达到目的?
#include "stdio.h"
main ()
{FILE *in,*out;
char ch,infile[10],outfile[10];
printf ("Enter the infile name:\n");
scanf ("%s",infile);
printf ("Enter the outfile name:\n");
scanf("%s",outfile);
if((in=fopen (infile,"r"))==NULL)
{ printf("cannot open infile\n");
exit(0);
}
if ((out=fopen (outfile,"w"))==NULL)
{printf ("cannot open outfile\n");
exit(0);
}
while(!feof(in))fputc(fgetc(in),out);
fclose(in);
fclose(out);
}
该在哪里补充,if之前还是while之后

C语言里的system("");函数可以执行命令行的几乎所有指令,把命令行输入的内容作为参数传入即可。复制文件的话 应该是:copy 源文件 目的路径。
例如命令行里的 copy c:\test.txt d:\text.txt,
也就是C语言里的:system("copy c:\test.txt d:\text.txt");
或者这样
char c[50] = "copy c:\test.txt d:\text.txt";
system(c);
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-02
包含库stdlib.h
然后调用system函数,使用命令行来解决。
copy:复制文件。
md:创建文件夹

在while之后执行:
system("md 你要复制到的路径");
system("copy 输入文件 输出文件");
注意:输出文件必须包括路径。
至于system参数中字符串的合并就你自己弄咯!^_^追问

该在哪里补充,if之前还是while之后

追答

在fclose(out);之后,因为要防止文件丢失

我说清楚点:

其实这个功能没必要这么麻烦来实现的。只需要使用DOS或Linux命令就能实现的。

完整伪程序:

#include <stdlib.h>

int main(void)
{
    system("md YourFolder");
    system("copy infile outfile");
    return 0;
}

之所以使用命令是因为C语言里面好像没有创建文件夹的函数,如果非要使用就只能调用API了。

本回答被提问者采纳
第2个回答  2013-07-19
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *in,*out;
char infile[50],outfile[50];
printf ("Enter the infile name:");
scanf ("%s",infile);
printf ("Enter the outfile name:");
scanf("%s",outfile);
if((in = fopen(infile,"r")) == NULL) {
printf("cannot open %s.\n",infile);
exit(0);
}
if((out = fopen (outfile,"w"))==NULL) {
printf ("cannot open %s.\n",outfile);
exit(0);
}
while(!feof(in))fputc(fgetc(in),out);
fclose(in);
fclose(out);
return 0;
}

相似回答