Home > Back-end >  How to use C language fopen? To solve the
How to use C language fopen? To solve the

Time:10-12

This is my code, why can't open the file?
#include
#include

Int main ()
{
The FILE * fp.
Char ch, filename [10];
Printf (" please enter the file name: ");
The scanf (" % s ", filename);
If ((fp=fopen (filename, "w"))==NULL);
{
Printf (" unable to open the file \ n ");
The exit (0);
}
Ch=getchar ();
Printf (" please enter a string prepare stored in the disk (end with a #) : ");
Ch=getchar ();
While (ch!='#')
{
Fputc (ch, fp);
Putchar (ch);
Ch=getchar ();
}
The fclose (fp);
Putchar (10);


return 0;
}
The great god give advice or comments, thank you.

CodePudding user response:

Example: in the BCB help
 
/* fopen example */
/* the Program to create backup of a file */

#include

Int main (void)
{
In the FILE * and * out;

If ((=in fopen (" TESTFILE. DAT ", "rt"))
==NULL)
{
Fprintf (stderr, "always open input file. \ n");
return 1;
}

If ((out=fopen (" TESTFILE. BAK ", "wt"))
==NULL)
{
Fprintf (stderr, "always open the output file. \ n");
return 1;
}

while (! The feof (in))
Fputc (fgetc (in), out);

The fclose (in);
The fclose (out);

return 0;
}

CodePudding user response:

You put the following two lines commented out running again see

Printf (" unable to open the file \ n ");
The exit (0);

CodePudding user response:

Because of you
 
If ((fp=fopen (filename, "w"))==NULL);//add a semicolon here; To remove the
the semicolon//so no matter how the judgment results, down to perform, so always prompt can't open the file

CodePudding user response:

Also copy to estimate, the errors were copied

CodePudding user response:

See the MSDN

CodePudding user response:

If and for statement semicolon after pure a slip of the pen, pit a lot of people.

CodePudding user response:

Good pit semicolon

CodePudding user response:

You lack the basic skills
  • Related