user-0 提问: c语言中visual studio中用fopen打不开文件

回复:
user: user-1
content: C和C++ 完整教程：https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++ 算法完整教程：https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: 引用 3 楼 qzjhjxj 的回复:修改如下，供参考：#include<stdio.h> #include<stdlib.h> #include<string.h>  void copyFile(char infile[],char outfile[]) {      FILE *fp1,*fp2;      char line[256];      if((fp1=fopen(infile,"r"))==NULL){         printf("Can't open file %s!\n",infile);         exit(1);      }      if((fp2=fopen(outfile,"w"))==NULL){         printf("Can't open file %s!\n",outfile);         exit(2);      }      while(fgets(line,100,fp1)!=NULL){          puts(line);          fputs(line,fp2);      }      fclose(fp1);      fclose(fp2); }   int main() {                          //FILE *fp1,*fp2;      char infile[30],outfile[30];                          //char *p=infile;                          //char *q=outfile;                         //int  i;      printf("请输入需要读取的文件名:");      gets(infile);                         //for(i=0;p<infile+30;p=p+i,i++){                         //    if(*p=='\n')*p='\0';                         //}      printf("请输入输出的文件名:");      gets(outfile);                        //for(i=0;q<outfile+30;q=q+i,i++){                        //    if(*q=='\n')*q='\0';                        //}                        //printf("需要读取的文件名为:%s",infile);                        //putchar('\n');      copyFile(infile,outfile);                        //fclose(fp1);                        //fclose(fp2);            return 0; } 使用fputs函数会引入额外的'\n'，因为fgets会有'\n'，而fputs又会加'\n'

user: user-2
content: 引用 3 楼 qzjhjxj 的回复:修改如下，供参考：#include<stdio.h> #include<stdlib.h> #include<string.h>  void copyFile(char infile[],char outfile[]) {      FILE *fp1,*fp2;      char line[256];      if((fp1=fopen(infile,"r"))==NULL){         printf("Can't open file %s!\n",infile);         exit(1);      }      if((fp2=fopen(outfile,"w"))==NULL){         printf("Can't open file %s!\n",outfile);         exit(2);      }      while(fgets(line,100,fp1)!=NULL){          puts(line);          fputs(line,fp2);      }      fclose(fp1);      fclose(fp2); }   int main() {                          //FILE *fp1,*fp2;      char infile[30],outfile[30];                          //char *p=infile;                          //char *q=outfile;                         //int  i;      printf("请输入需要读取的文件名:");      gets(infile);                         //for(i=0;p<infile+30;p=p+i,i++){                         //    if(*p=='\n')*p='\0';                         //}      printf("请输入输出的文件名:");      gets(outfile);                        //for(i=0;q<outfile+30;q=q+i,i++){                        //    if(*q=='\n')*q='\0';                        //}                        //printf("需要读取的文件名为:%s",infile);                        //putchar('\n');      copyFile(infile,outfile);                        //fclose(fp1);                        //fclose(fp2);            return 0; }   while(fgets(line, sizeof(line),fp1)!=NULL) 改一下～

user: user-3
content: 大概率是路径不对，你访问的文件不在你以为的位置。

user: user-4
content: 修改如下，供参考：#include<stdio.h> #include<stdlib.h> #include<string.h>  void copyFile(char infile[],char outfile[]) {      FILE *fp1,*fp2;      char line[256];      if((fp1=fopen(infile,"r"))==NULL){         printf("Can't open file %s!\n",infile);         exit(1);      }      if((fp2=fopen(outfile,"w"))==NULL){         printf("Can't open file %s!\n",outfile);         exit(2);      }      while(fgets(line,100,fp1)!=NULL){          puts(line);          fputs(line,fp2);      }      fclose(fp1);      fclose(fp2); }   int main() {                          //FILE *fp1,*fp2;      char infile[30],outfile[30];                          //char *p=infile;                          //char *q=outfile;                         //int  i;      printf("请输入需要读取的文件名:");      gets(infile);                         //for(i=0;p<infile+30;p=p+i,i++){                         //    if(*p=='\n')*p='\0';                         //}      printf("请输入输出的文件名:");      gets(outfile);                        //for(i=0;q<outfile+30;q=q+i,i++){                        //    if(*q=='\n')*q='\0';                        //}                        //printf("需要读取的文件名为:%s",infile);                        //putchar('\n');      copyFile(infile,outfile);                        //fclose(fp1);                        //fclose(fp2);            return 0; }

user: user-5
content: 文件名输类似d:\mydir\filename.ext这样的全路径

user: user-5
content: 30改为256