Here's my dilemma. I have a file, and wish to read in all characters up until the program hits a '#', and ignore everything on that line after the '#'. For example
0 4001232 0 #comment, discard
This is frustrating, as it feels like there is a very simple solution. Thanks!
There are plenty of ways and examples of how to do it. Usually, the idea is to have a variable that holds the state (before #, after # , after \n etc.) and run in a while loop until EOF. an example you can see here it's a program to remove C comments, but the idea is the same.
Read a line using fgets, read through this line till you get a '#' character.
Read an another line...
This is a bit more of a pre-processing than parsing question in my mind. Anyway there are number of tool and commands that specialize in doing just what you ask. If possible, it is probably best to use them.
If however you need or want to do so inside your code than the general method for doing so is, as was already mentioned, to keep the current state your in and handle any new character according to the state. This is a very good general method and is highly recommended, especially is there is more pre-processing that needs to be done.
If however this is the absolutely the only thing you what to do, than you can do something a little bit better and forgo the state with a code like this:
The solution depends on how you are "reading" that.
I could, for example, just remove all of those comments with
sed 's/#.*//' <infile >outfile
in bash.EDIT: However, if I was parsing it manually, I could simply (in my loop for parsing it) have
which would stop parsing that line by exiting the loop.