C++ I/O for parsing a simple file

Discussion in 'C++' started by Nafai, Apr 9, 2005.

  1. Nafai

    Nafai Guest

    Hello!

    I'd be very grateful if you can give some advice to me. I explain my
    problem:

    I want to read a file whose structure I need to be like this:

    #comments...
    value1,value2,value3,...,valueN # comments...
    #comments...
    value1,value2,value3,...,valueN # comments...
    ....
    value1,value2,value3,...,valueN # comments...

    So what I want is to be able to read a file in which there are some
    lists of values separated by new line. Each value is separated by a
    comma. And there may be comments at the beggining or at the end of any line.

    I can't use getline(...,...,'\n') because I have to take care of '#'
    I can't use getline(...,...,'\#') because comments are not compulsory.

    So what do you suggest?

    thanks.
     
    Nafai, Apr 9, 2005
    #1
    1. Advertising

  2. Nafai

    Val Guest

    |
    | I can't use getline(...,...,'\n') because I have to take care of '#'
    | I can't use getline(...,...,'\#') because comments are not compulsory.
    |

    Yes you can. Then check for the existance of "#". It it exists, remove it and everything that comes after that.
     
    Val, Apr 9, 2005
    #2
    1. Advertising

  3. Nafai

    Alvin Beach Guest

    Nafai wrote:

    > Hello!
    >
    > I'd be very grateful if you can give some advice to me. I explain my
    > problem:
    >
    > I want to read a file whose structure I need to be like this:
    >
    > #comments...
    > value1,value2,value3,...,valueN # comments...
    > #comments...
    > value1,value2,value3,...,valueN # comments...
    > ...
    > value1,value2,value3,...,valueN # comments...
    >
    > So what I want is to be able to read a file in which there are some
    > lists of values separated by new line. Each value is separated by a
    > comma. And there may be comments at the beggining or at the end of any
    > line.
    >
    > I can't use getline(...,...,'\n') because I have to take care of '#'
    > I can't use getline(...,...,'\#') because comments are not compulsory.
    >
    > So what do you suggest?
    >
    > thanks.


    I would use an fstream and read a line at a time (so basically the
    getline()). Then I would check the first character, if it's a # then the
    line is a comment. If not, the the line contains values a possible comment.
    How you handle when a line doesn't start with a # can be done 101 different
    ways.
     
    Alvin Beach, Apr 9, 2005
    #3
  4. Nafai

    Nafai Guest

    > I would use an fstream and read a line at a time (so basically the
    > getline()). Then I would check the first character, if it's a # then the
    > line is a comment. If not, the the line contains values a possible comment.
    > How you handle when a line doesn't start with a # can be done 101 different
    > ways.


    I asked this question because there are many files with this structure
    and I thought there would be a "standard" way of doing sth like this. I
    can do it my way but I would like to hear some tips or frequent ways of
    doing this.

    Thanks.
     
    Nafai, Apr 11, 2005
    #4
  5. Nafai

    Nafai Guest

    I asked this question because there are many files with this structure
    and I thought there would be a "standard" way of doing sth like this. I
    can do it my way but I would like to hear some tips or frequent ways of
    doing this.

    Thanks.


    Alvin Beach escribió:
    > Nafai wrote:
    >
    >
    >>Hello!
    >>
    >>I'd be very grateful if you can give some advice to me. I explain my
    >>problem:
    >>
    >>I want to read a file whose structure I need to be like this:
    >>
    >>#comments...
    >>value1,value2,value3,...,valueN # comments...
    >>#comments...
    >>value1,value2,value3,...,valueN # comments...
    >>...
    >>value1,value2,value3,...,valueN # comments...
    >>
    >>So what I want is to be able to read a file in which there are some
    >>lists of values separated by new line. Each value is separated by a
    >>comma. And there may be comments at the beggining or at the end of any
    >>line.
    >>
    >>I can't use getline(...,...,'\n') because I have to take care of '#'
    >>I can't use getline(...,...,'\#') because comments are not compulsory.
    >>
    >>So what do you suggest?
    >>
    >>thanks.

    >
    >
    > I would use an fstream and read a line at a time (so basically the
    > getline()). Then I would check the first character, if it's a # then the
    > line is a comment. If not, the the line contains values a possible comment.
    > How you handle when a line doesn't start with a # can be done 101 different
    > ways.
     
    Nafai, Apr 11, 2005
    #5
    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. GIMME
    Replies:
    2
    Views:
    877
    GIMME
    Feb 11, 2004
  2. VB Programmer

    Example needed: simple XML file and parsing

    VB Programmer, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    797
    Craig Deelsnyder
    Jan 6, 2004
  3. Naren
    Replies:
    0
    Views:
    585
    Naren
    May 11, 2004
  4. Fuzzyman
    Replies:
    4
    Views:
    346
    Thomas Heller
    May 28, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    465
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page