How to read input with comments led by some commenting symbol

Discussion in 'C++' started by Jayden Shui, Nov 14, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    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
    Jayden Shui, Nov 14, 2011
    #1
    1. Advertising

  2. On 11/14/2011 10:01 AM, Jayden Shui wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 14, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Nov 14, 10:13 am, Victor Bazarov <> wrote:
    > On 11/14/2011 10:01 AM, Jayden Shui wrote:
    >
    > > 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
    > --
    > I do not respond to top-posted replies, please don't ask


    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
    Jayden Shui, Nov 14, 2011
    #3
  4. Jayden Shui

    Larry Evans Guest

    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:



    with a subject starting with [iostreams].

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

    -regards,
    Larry
    Larry Evans, Nov 14, 2011
    #4
  5. Jayden Shui

    Jayden Shui Guest

    On Nov 14, 10:54 am, Larry Evans <> wrote:
    > 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:
    >
    >  
    >
    > 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
    Jayden Shui, Nov 14, 2011
    #5
  6. Jayden Shui

    Guest

    On Nov 14, 10:34 am, Jayden Shui <> wrote:
    > On Nov 14, 10:13 am, Victor Bazarov <> wrote:
    >
    > > On 11/14/2011 10:01 AM, Jayden Shui wrote:

    >
    > > > 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
    > > --
    > > I do not respond to top-posted replies, please don't ask

    >
    > 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
    , Nov 14, 2011
    #6
  7. Jayden Shui

    Jorgen Grahn Guest

    On Mon, 2011-11-14, Jayden Shui wrote:
    > On Nov 14, 10:13 am, Victor Bazarov <> wrote:
    >> On 11/14/2011 10:01 AM, Jayden Shui wrote:
    >>
    >> > 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.


    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 14, 2011
    #7
  8. On 11/14/2011 3:12 PM, Jorgen Grahn wrote:
    > On Mon, 2011-11-14, Jayden Shui wrote:
    >> On Nov 14, 10:13 am, Victor Bazarov<> wrote:
    >>> On 11/14/2011 10:01 AM, Jayden Shui wrote:
    >>>
    >>>> 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.

    >
    >> 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!


    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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 14, 2011
    #8
  9. Jayden Shui

    Guest

    On Nov 14, 3:12 pm, Jorgen Grahn <> wrote:
    > On Mon, 2011-11-14, Jayden Shui wrote:
    > > On Nov 14, 10:13 am, Victor Bazarov <> wrote:
    > >> On 11/14/2011 10:01 AM, Jayden Shui wrote:

    >
    > >> > 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.

    > > 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
    >
    > --
    >   // Jorgen Grahn <grahn@  Oo  o.   .     .
    > \X/     snipabacken.se>   O  o   .


    I totally concur with you comment on iostreams. Especially when most
    of my work is done reading line by line.
    , Nov 14, 2011
    #9
  10. Jayden Shui

    Jorgen Grahn Guest

    On Mon, 2011-11-14, wrote:
    > On Nov 14, 3:12 pm, Jorgen Grahn <> wrote:

    ....
    >> 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!


    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 14, 2011
    #10
  11. Jayden Shui

    Jorgen Grahn Guest

    On Mon, 2011-11-14, Victor Bazarov wrote:
    > On 11/14/2011 3:12 PM, Jorgen Grahn wrote:
    >> On Mon, 2011-11-14, Jayden Shui wrote:
    >>> On Nov 14, 10:13 am, Victor Bazarov<> wrote:
    >>>> On 11/14/2011 10:01 AM, Jayden Shui wrote:
    >>>>
    >>>>> 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.

    >>
    >>> 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!

    >
    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 15, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. network lines

    VHDL code to light up LED???

    network lines, May 25, 2004, in forum: VHDL
    Replies:
    8
    Views:
    8,491
    Symon
    May 27, 2004
  2. Flemming Hansen

    LED decoder with CoolRunner II

    Flemming Hansen, Jan 9, 2006, in forum: VHDL
    Replies:
    5
    Views:
    1,595
    Mike Treseler
    Jan 23, 2006
  3. Replies:
    1
    Views:
    552
    Chris Lambacher
    Jul 31, 2006
  4. =?iso-8859-1?q?J=FCrgen_Gerstacker?=

    A swing/awt component that looks like a LED

    =?iso-8859-1?q?J=FCrgen_Gerstacker?=, Dec 15, 2006, in forum: Java
    Replies:
    2
    Views:
    1,046
    =?iso-8859-1?q?J=FCrgen_Gerstacker?=
    Dec 17, 2006
  5. Amit

    string recognize and led

    Amit, Nov 8, 2007, in forum: VHDL
    Replies:
    1
    Views:
    372
Loading...

Share This Page