My classmate write code:
#include
#include
Int main ()
{
int a;
Char b;
Float c;
The FILE * fp.
The scanf (" % f, % d, % c ", & amp; A, & amp; B, & amp; C);
If ((fopen (" int. TXT ", "w"))==NULL)
{
Printf (" always open a file \ n ");
exit(0);
}
Fprintf (fp, "% d, % s, % f", a, b, c);
fclose(fp);
return 0;
}
Finally execution can only create a file, but the file in blank,
According to the online classes I write:
#include
#include
Int main ()
{
Int a, b;
Char STR [20], [20] s.
Float x, y;
The FILE * fp.
The scanf (" % d % s % f ", & amp; A, STR, & amp; X);
If ((fp=fopen (" int. TXT ", "w"))==NULL)
{
Printf (" always open a file \ n ");
exit(0);
}
Fprintf (fp, "% d % % s. 1 f", a, STR, x);
fclose(fp);
return 0;
}
The last execution success
Help:
These two code, the difference is?
Why the first can't perform a success?
Really don't understand to seek help from bosses
CodePudding user response:
He didn't seem to point to any first of fpCodePudding user response:
fprintf (fp, "% d, % s, % f", a, b, c);
To:
fprintf (fp, "% f, % d, % c, a, b, c);
Don't you didn't find the first program, so the return value of fopen assigned to fp?
CodePudding user response:
You are the first codeThe scanf (" % f, % d, % c ", & amp; A, & amp; B, & amp; C);
This way, the scanf function using commas, also want to enter a comma when input
CodePudding user response: