Thursday, 21 October 2010

C programming - reading line from file

Suppose you want to 'feed' a program a file with data. In this case it is often convenient when you add some comments to the data, so you will understand it in the future. So, the file may look like

data data
comments comments
more comments

In the program reading this data file, you may want to 'skip' the lines containing comments. One way of doing this is reading the entire line, until end-of-line character.

This can easily be done using
fscanf(fs, "\n%[^\n]", dummy);

where fs is file handlers, and dummy a (large enough) string to hold the line.

Idea was found on programmers heaven. As it took me quite some searching for a solution, henceforth this post.

No comments:

Post a Comment