方法一:当前工程下的文件
fopen("demo.txt","rt")
方法二:当锋吵唤前工程下的 test文件夹 中文件
fopen(".\\test\\demo.txt","rt")
方法三:绝对路径,D盘下project文件夹中的文碰消件
fopen("d:\\project\\demo.txt","rt")
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欢迎分享,转载请注明来源:内存溢出
评论列表(0条)