I want to read line-by-line from a given input file,, process each line (i.e. its words) and then move on to other line…
So i am using fscanf(fptr,’%s’,words) to read the word and it should stop once it encounters end of line…
but this is not possible in fscanf, i guess… so please tell me the way as to what to do…
I should read all the words in the given line (i.e. end of line should be encountered) to terminate and then move on to other line, and repeat the same process..
Use fgets(). Yeah, link is to cplusplus, but it originates from c
stdio.h.You may also use
sscanf()to read words from string, or juststrtok()to separate them.In response to comment: this behavior of
fgets()(leaving\nin the string) allows you to determine if the actual end-of-line was encountered. Note, thatfgets()may also read only part of the line from file if supplied buffer is not large enough. In your case – just check for\nin the end and remove it, if you don’t need it. Something like this:Simple as that.