Search code examples
cgetchar

Reading input from file in C


Okay so I have a file of input that I calculate the amount of words and characters in each line with success. When I get to the end of the line using the code below it exits the loop and only reads in the first line. How do I move on to the next line of input to continue the program?

EDIT: I must parse each line separately so I cant use EOF

while( (c = getchar()) != '\n')

Solution

  • Change '\n' to EOF. You're reading until the end of the line when you want to read until the end of the file (EOF is a macro in stdio.h which corresponds to the character at the end of a file).

    Disclaimer: I make no claims about the security of the method.