How to print result to standard output?
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"
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).
have a look at the fgets specification, better:
while( fgets(line, MAXBUF, fp)!= NULL) {
puts(line);
}
or
while( fgets(line, MAXBUF, fp) ) {
puts(line);
}
精彩评论