fp=fopen(filename,"wb")\x0d\x0a里的filename就表示了
文件的
路径及
文件名,所以要把输入的文件名和文件路径拼接起来,计算出这个filename\x0d\x0a最好过滤一下别让路径和文件名中有非法字符,比如:\/+什么的。\x0d\x0ascanf也限制一下长度。\x0d\x0ascanf("%19s",filename)\x0d\x0ascanf("%19s",path)\x0d\x0a参考如下:\x0d\x0a#include \x0d\x0a#include \x0d\x0aint main(void)\x0d\x0a{\x0d\x0a FILE *fp\x0d\x0a char ch, filename[20], path[20],buffer[40]\x0d\x0a printf("Enter the file name: ")\x0d\x0a scanf("%s",filename)\x0d\x0a printf("Enter the path: ")\x0d\x0a scanf("%s",path)\x0d\x0a sprintf(buffer, "%s\\%s", path,filename)\x0d\x0a printf("\nto end input ,press Ctrl+Z in newline,then ENTER:\n")\x0d\x0a if((fp=fopen(buffer,"wt+"))==NULL)\x0d\x0a {\x0d\x0a printf("no such path, \nstrike any key exit!")\x0d\x0a getch()\x0d\x0a exit(1)\x0d\x0a }\x0d\x0a while ((ch=getchar())!=-1) ch=fputc(ch,fp)\x0d\x0a fclose(fp)\x0d\x0a printf("==================================\n\n")\x0d\x0a printf("file path \"%s\" \nfile name \"%s\":\nand its content:\n",path,filename)\x0d\x0a fp=fopen(buffer,"rt")\x0d\x0a while ((ch=fgetc(fp))!=-1) putchar(ch)\x0d\x0a fclose(fp)\x0d\x0a return 0分类: 电脑/网络 >>程序设计 >>其他编程语言
问题描述:
C语言的生成文件能存放到指定的目录吗?比如F盘E盘?
解析:
能!
FILE *fp
if((fp=fopen("F:\\KAKA\\JXD\","w"))==NULL)/*双\\(转义字符)""为当前目录*/
{ printf("Can't open file!")/* 错误处理*/
return
}
else /*code*/
评论列表(0条)