Code:
#include<stdio.h>
/* If you are going to use exit(), include
** stdlib.h. You might as well include it
** anyway so you can use the return status
** macros
*/
#include<stdlib.h>
/* void main is totally wrong, sorry */
int main(void)
{
FILE *infile;
/* Allocate memory or use an array, segfaults
** are no fun.
*/
char line[BUFSIZ];
if((infile = fopen("info.txt", "r"))==NULL )
{
/* perror is much more suited to error reporting
** after standard function calls, you get better
** information. And since you're exiting anyway,
** why not just return and use a more informative
** condition so that readers know the program is
** terminating due to error.
*/
perror("Cannot open input file");
return EXIT_FAILURE;
}
/* fgets is much better for line input, either from
** a file or from stdin.
*/
while(fgets(line, sizeof line, infile) != NULL)
printf("%s\n", line);
/* Always close your files */
fclose(infile);
/* main returns an int, keep it portable by using
** either 0, EXIT_SUCCESS, or EXIT_FAILURE
*/
return EXIT_SUCCESS;
}
-Prelude