
fp=fopen(filename,"wb");
里的filename就表示了文件的路径及文件名,所以要把输入的文件名和文件路径拼接起来,计算出这个filename
最好过滤一下别让路径和文件名中有非法字符,比如:\/+<>什么的。
scanf也限制一下长度。
scanf("%19s",filename);
scanf("%19s",path);
参考如下:
#include <stdioh>
#include <stdlibh>
int main(void)
{
FILE fp;
char ch, filename[20], path[20],buffer[40];
printf("Enter the file name: ");
scanf("%s",filename);
printf("Enter the path: ");
scanf("%s",path);
sprintf(buffer, "%s\\%s", path,filename);
printf("\nto end input ,press Ctrl+Z in newline,then ENTER:\n");
if((fp=fopen(buffer,"wt+"))==NULL)
{
printf("no such path, \nstrike any key exit!");
getch();
exit(1);
}
while ((ch=getchar())!=-1) ch=fputc(ch,fp);
fclose(fp);
printf("==================================\n\n");
printf("file path \"%s\" \nfile name \"%s\":\nand its content:\n",path,filename);
fp=fopen(buffer,"rt");
while ((ch=fgetc(fp))!=-1) putchar(ch);
fclose(fp);
return 0;
#include <windowsh>
#include <iostream>
using namespace std;
void main()
{
char str[100] = "COPY ";//
char name[20];
cin>>name; //输入文件名的完全路径(不用双斜杠)
strcat(str,name);
strcat(str," C:\\我的文档\\"); //注意要有空格
cout<<str; //看下命令语句是否正确
system(str);
}
这个替换掉system("copy [dangqian] [mubiao]");
-------------------------------------------------------------
char str[0];
strcat(str,"copy ");
strcat(str, dangqian);
strcat(str," ");
strcat(str, mubiao);
system(str);
-------------------------------------------------------------
#include<stdioh>
#include<stdlibh>
#include<stringh>
int main()
{ char s[300]="copy ",f1[100],f2[100],f3[100];
printf("File1 name:");
scanf("%s",f1);
printf("File2 name:");
scanf("%s",f2);
printf("Dest File name:");
scanf("%s",f3);
strcat(s,f1);
strcat(s,"/b+");
strcat(s,f2);
strcat(s,"/b ");
strcat(s,f3);
puts(s);
system(s);
return 0;
}
以上就是关于c语言保存文件到指定的路径和文件名全部的内容,包括:c语言保存文件到指定的路径和文件名、c++语言 指定文件复制到文件夹、c语言如何将当前正在运行的exe文件复制到另一个文件夹等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)