September 2001
Beginner
970 pages
23h 20m
English
| 1: | What's wrong with this program?
int main(void)
{
int * fp;
int k;
fp = fopen("gelatin");
for (k = 0; k < 30; k++)
fputs(fp, "Nanette eats gelatin.");
fclose("gelatin");
return 0;
}
|
| A1: | It should have #include <stdio.h> for its file definitions. It should declare fp a file pointer: FILE *fp;. The function fopen() requires a mode: fopen("gelatin", "w"), or perhaps the "a" mode. The order of the arguments to fputs() should be reversed. For clarity, the output string should have a newline because fputs() doesn't add one automatically. The fclose() function requires a file pointer, not a filename: fclose(fp);. Here is a corrected version:
#include <stdio.h> int main(void) { FILE * fp; int k; fp = fopen("gelatin", "w"); for (k = 0; k ... |
Read now
Unlock full access