Hey guys im trying to read in a file and list numbers to each line in the file but for some reason im getting a runtime logical error inside my while loop.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int line_no = 0;
int input_char = 0;
int nl = 1;
FILE *fp;
if(argc !=2) /* check for 2 arguments*/
{
fprintf(stderr, "invalid usage :%s\n", argv[0]);
return 1; /* stop processing if failes */
}
if((fp = fopen(argv[1], "r")) == NULL)
/* opens file for reading and checks if it exists */
{
fprintf(stderr, "invalid usage: %s\n", argv[0]);
return 1; /* stop processing if fails*/
}
while(input_char !=EOF)
/* keep asking for input while not end of file*/
{
if ( nl )
{
printf("\n%d ", line_no);
line_no++;
}
else
{
putc(input_char, stdout);
input_char = getc(fp);
nl = input_char == '\n';
}
}
fclose(fp);
return EXIT_SUCCESS;
}