Home > Back-end >  Visual studio in c language using the fopen can't open the file
Visual studio in c language using the fopen can't open the file

Time:04-26

CodePudding user response:

30 to 256

CodePudding user response:

File name lose similar d: \ mydir \ filename ext so that the full path

CodePudding user response:

Modified as follows, for your reference:
 # include 
#include
#include

Void used by copyFile (char infile [], char outfile [])
{
The FILE * fp1, * fp2;
Char line [256].
If ((fp1=fopen (infile, "r"))==NULL) {
Printf (" Can 't open the file % s! \ n ", infile);
exit(1);
}
If ((fp2=fopen (outfile, "w"))==NULL) {
Printf (" Can 't open the file % s! \ n ", outfile);
The exit (2);
}
While (the fgets (line, 100, fp1)!=NULL) {
Puts (line);
The fputs (line, fp2);
}
The fclose (fp1);
The fclose (fp2);
}


Int main ()
{
//fp1 FILE * and * fp2;
Outfile char infile [30], [30].
//char * p=infile;
//char * q=outfile;
//int I;
Printf (" please enter the need to read the file name: ");
Gets (infile);
//for (I=0; p//if (* p=='\ n') * p='\ 0';
//}
Printf (" please input and output file name: ");
Gets (outfile);
//for (I=0; Q//if q=='\ n') (* * q='\ 0';
//}
//printf (" need to read the file called: % s ", infile);
//putchar (' \ n ');
Used by copyFile (infile outfile);
//fclose (fp1);
//fclose (fp2);

return 0;
}

CodePudding user response:

The
reference 3 floor QZJHJXJ response:
modify as follows, for reference:
 # include 
#include
#include

Void used by copyFile (char infile [], char outfile [])
{
The FILE * fp1, * fp2;
Char line [256].
If ((fp1=fopen (infile, "r"))==NULL) {
Printf (" Can 't open the file % s! \ n ", infile);
exit(1);
}
If ((fp2=fopen (outfile, "w"))==NULL) {
Printf (" Can 't open the file % s! \ n ", outfile);
The exit (2);
}
While (the fgets (line, 100, fp1)!=NULL) {
Puts (line);
The fputs (line, fp2);
}
The fclose (fp1);
The fclose (fp2);
}


Int main ()
{
//fp1 FILE * and * fp2;
Outfile char infile [30], [30].
//char * p=infile;
//char * q=outfile;
//int I;
Printf (" please enter the need to read the file name: ");
Gets (infile);
//for (I=0; p//if (* p=='\ n') * p='\ 0';
//}
Printf (" please input and output file name: ");
Gets (outfile);
//for (I=0; Q//if q=='\ n') (* * q='\ 0';
//}
//printf (" need to read the file called: % s ", infile);
//putchar (' \ n ');
Used by copyFile (infile outfile);
//fclose (fp1);
//fclose (fp2);

return 0;
}


 while (the fgets (line, sizeof (line), fp1)!=NULL) 

To change ~

CodePudding user response:

The
reference 3 floor QZJHJXJ response:
modify as follows, for reference:
 # include 
#include
#include

Void used by copyFile (char infile [], char outfile [])
{
The FILE * fp1, * fp2;
Char line [256].
If ((fp1=fopen (infile, "r"))==NULL) {
Printf (" Can 't open the file % s! \ n ", infile);
exit(1);
}
If ((fp2=fopen (outfile, "w"))==NULL) {
Printf (" Can 't open the file % s! \ n ", outfile);
The exit (2);
}
While (the fgets (line, 100, fp1)!=NULL) {
Puts (line);
The fputs (line, fp2);
}
The fclose (fp1);
The fclose (fp2);
}


Int main ()
{
//fp1 FILE * and * fp2;
Outfile char infile [30], [30].
//char * p=infile;
//char * q=outfile;
//int I;
Printf (" please enter the need to read the file name: ");
Gets (infile);
//for (I=0; p//if (* p=='\ n') * p='\ 0';
//}
Printf (" please input and output file name: ");
Gets (outfile);
//for (I=0; Q//if q=='\ n') (* * q='\ 0';
//}
//printf (" need to read the file called: % s ", infile);
//putchar (' \ n ');
Used by copyFile (infile outfile);
//fclose (fp1);
//fclose (fp2);

return 0;
}

Use fputs functions will introduce additional '\ n', because the fgets can have '\ n', while the fputs will add '\ n'

CodePudding user response:

Big probability is the right path, you access the files is not you think the location of the,
  • Related