C++ code for reading text from file? (help)

Hello. I was wondering if anyone could tell me how to do this.

Read the text after the inputted label from this file:
input.txt
1
2
3
Msg1::"Hello world!"

Msg2::"Howdy!"

Does anyone know how to input Msg2 for example and std::cout
the message after Msg2?

If clarification is needed, tell me.

Thanks.
roughly, not tested etc..

ifstream inf(filename);
string s;
getline(inf, s);
cout << s.substr(6) << endl; //should skip the msg1:: part and print the rest.
Thanks. I will try this.

EDIT: Tried it, it printed Msg1 instead of Msg2.
Can I make it skip 2 lines?
Last edited on
How about a function that reads lines that start with '#'?

1
2
#Label1
#Label2 
Last edited on
just do getlines and discard the ones you do not want to skip a line or two.
what the line starts with does not matter.
if you need it to ignore any of a bunch of starting tokens, you need a little more smarts than the hard coded skip 6 letters, but the general idea is the same: figure out how much you want to skip (to the first space? until you hit a " character? whatever rules work for you) and then pull the substring you want out of it.

you can get very fancy, using regx or finite state machine or other parsers. I generally take the path of least effort, until the problem is shown to be difficult enough to get out the big tools. A guy I worked with would use the ancient C language compiler compiler stuff (yacc/lex/etc) to make parsers.
Last edited on
Topic archived. No new replies allowed.