Reading a formatted text file

Hello everyone.
I'm still quite new to C++.
I have to read a file like this:

1
2
3
4
5
6
7
8
9
10
11
12
 NAME example
INPUTS 3
OUTPUTS 2
NETS 3
// this is a comment
GATES
Logic Simulator (LS)
XOR2 U0 I0, I1, N0
XOR2 U1 N0, I2, O0
AND2 U2 N0, I2, N1
AND2 U3 I0, I1, N2
OR2 U4 N1, N2, 01


Is there a simple way to read a line, ignoring comments, and then taking single words?
I can only find awful ways to do it, all more plain C oriented.
Thank you very much.
Last edited on
Have you looked into using a regex function template?

The following is a page that might be useful on this site, that might give you a nice handle on doing what you want better:
http://www.cplusplus.com/reference/regex/basic_regex/

In that reference page there is a grep function that allows you to check a sub-string of a line. *Edit: It looks like the grep was a internal of the class regex in C++. Sorry. I think you still might be able to use the regex template to make it a little more elegant. Please let me know if you find a better way to do it.*

Also this site gives you a general handle on what kind of regex syntax you have available to you:
http://www.alertsite.com/help/RegexMatching.html

Also here is some psuedo code that also might help:
1
2
3
4
5
6
7
8
while there are lines in the file:
	regex label equals "\\w+\\d*\\s\\d*";
	if line sub-string matches above defined label:
		then store data;
	else if line has a sub-string that matches regex code:
		then store as title;
	otherwise:
		ignore.


Hope that helps,

- Hirokachi
Last edited on
Topic archived. No new replies allowed.