How to read input with comments led by some commenting symbol

J

Jayden Shui

Hello All,

I am thinking about using streams to read input with comments led by
some commenting symbol such as '#'. I appreciate your suggestion.

Best regards,

Jayden
 
V

Victor Bazarov

I am thinking about using streams to read input with comments led by
some commenting symbol such as '#'. I appreciate your suggestion.

I suggest you get on with it, then.

V
 
J

Jayden Shui

I suggest you get on with it, then.

V

I have two methods to do it, but am not satisfied. One is developing a
manipulator ignore_comments and use it like:

cin >> ignore_comments >> data1 >> ignore_comments >> data2 >> ...

The other way is using boost filtering stream shown in

http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

I tried this method, but when I input by cin, I have to click ctrl+z
twice to stop the input and let the code continue to run. I don' know
why?

Thank you for your help!

Jayden
 
L

Larry Evans

On 11/14/11 09:34, Jayden Shui wrote:
[snip]
The other way is using boost filtering stream shown in

http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

I tried this method, but when I input by cin, I have to click ctrl+z
twice to stop the input and let the code continue to run. I don' know
why?

Try posting this question to the boost users list:

(e-mail address removed)

with a subject starting with [iostreams].

I'd guess you'd get more and possibly better responses than on
this list.

-regards,
Larry
 
J

Jayden Shui

On 11/14/11 09:34, Jayden Shui wrote:
[snip]
The other way is using boost filtering stream shown in
I tried this method, but when I input by cin, I have to click ctrl+z
twice to stop the input and let the code continue to run. I don' know
why?

Try posting this question to the boost users list:

  (e-mail address removed)

with a subject starting with [iostreams].

I'd guess you'd get more and possibly better responses than on
this list.

-regards,
Larry

Thank you very much!

Best regards,

Jayden
 
A

AnonMail2005

I have two methods to do it, but am not satisfied. One is developing a
manipulator ignore_comments and use it like:

    cin >> ignore_comments >> data1 >> ignore_comments >> data2 >> ....

The other way is using boost filtering stream shown in

   http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

I tried this method, but when I input by cin, I have to click ctrl+z
twice to stop the input and let the code continue to run. I don' know
why?

Thank you for your help!

Jayden

May I suggest you just write your own getline function? I don't know
your requirements but it seems more straightforward if you're doing
line based input.

There's a C++ library getline function that reads a line into a
std::string. It's very easy to use that inside your own getline
function which just skips comment lines however you define them.

HTH
 
J

Jorgen Grahn

I have two methods to do it, but am not satisfied. One is developing a
manipulator ignore_comments and use it like:

cin >> ignore_comments >> data1 >> ignore_comments >> data2 >> ...

The other way is using boost filtering stream shown in

http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

I just do it like I do it in Perl: read line by line, and for each line:
- does it contain a #? Then continue parsing the text between start
of line and the #.
- otherwise, parse the text between start of line and '\n' or
end-of-line

Has been good enough so far.

Never learned to use the fancy iostreams input stuff, and all the
questions about that around here makes me think that was the right
choice!

/Jorgen
 
V

Victor Bazarov

I just do it like I do it in Perl: read line by line, and for each line:
- does it contain a #? Then continue parsing the text between start
of line and the #.
- otherwise, parse the text between start of line and '\n' or
end-of-line

Has been good enough so far.

Never learned to use the fancy iostreams input stuff, and all the
questions about that around here makes me think that was the right
choice!

That should work just fine as long as there are no tokens that are
allowed to contain # that isn't the beginning of a "comment", like
string literals, for instance... ;-)

V
 
A

AnonMail2005

I just do it like I do it in Perl: read line by line, and for each line:
- does it contain a #?  Then continue parsing the text between start
  of line and the #.
- otherwise, parse the text between start of line and '\n' or
  end-of-line

Has been good enough so far.

Never learned to use the fancy iostreams input stuff, and all the
questions about that around here makes me think that was the right
choice!

/Jorgen

I totally concur with you comment on iostreams. Especially when most
of my work is done reading line by line.
 
J

Jorgen Grahn

I totally concur with you comment on iostreams. Especially when most
of my work is done reading line by line.

I should add that I /do/ like them for line-by-line input, and for
formatted output. It's the formatted input I don't like (and I don't
like fscanf() either).

Some reject iostreams entirely.

/Jorgen
 
J

Jorgen Grahn

That should work just fine as long as there are no tokens that are
allowed to contain # that isn't the beginning of a "comment", like
string literals, for instance... ;-)

Yes, thus the "good enough so far". Now that you mention it, I usually
just claim that

# this is a comment
foo # but this is not

I normally don't need line-by-line indentation, and I normally have no
need to allow a non-comment # first on a line.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top