c - How can I test if the standard input starts with a certain word? -


i writing c program parse input files can come in 2 different versions. need able deduce file format parsing files.

the first version list of numbers, 1 per line:

123 456 789 

the second type of file has 2 numbers per line , line @ top marking newest version of file format

#version 2 123 10 456 20 789 30 

what simplest way test file reading starts "#version n" line? if version line there want consume , if not don't want consume characters in input stream.

the problem think need way unread characters , know of ungetc, works single character , not whole line.

once read file, can set pointers original position.

c provides fseek change position of file pointers. once read file , found of different version, can use fseek set pointer @ beginning next read starts beginning of file.


Comments