tags:

views:

66

answers:

2
            char line[MAXBUF];
            char *result;

            while((result = fgets(line, MAXBUF, fp)) != NULL) {
                printf(result); 
            }

The following code doesn't work fully. Does anyone know how to print result?? MAXBUF is defined to be 1024 and fp is just a file pointer to some file. What im suppose to do in this assignment is read file and print the files output to standard output. Any ideas?

On the line printf(result) i get this error "warning: format not a string literal and no format arguments"

A: 

have a look at the fgets specification, better:

while( fgets(line, MAXBUF, fp)!= NULL) {
                puts(line); 
            }

or

while( fgets(line, MAXBUF, fp) ) {
                puts(line); 
            }
I don't see what this accomplishes besides adding an extra newline between each line. `result` may not be necessary, but it's not wrong.
Matthew Flaschen
+1  A: 

The following is what you want to do:

char line[MAXBUF];
char *result;

while((result = fgets(line, MAXBUF, fp)) != NULL) {
      printf("%s", line);
}

The fgets inputs the line (retaining the newline). You are checking result, which is correct. Theoretically, result should equal line. The printf doesn't have a '\n' because the newline character is retained from the fgets (see the manpage).

No One in Particular