Text File parsing

Discussion in 'C++' started by Imran, Aug 11, 2004.

  1. Imran

    Imran Guest

    hello all, I have to parse a text file and get some value in that.

    text file content is as follows.

    ####TEXT FILE CONTENT STARTS HERE #####
    /start
    first
    0x1234
    AC
    /end

    /start
    first
    0x12345
    AC
    /end

    /start
    first
    0x12344
    AC
    /end

    someotherdatahere
    ####TEXT FILE CONTENT ENDSHERE #####

    If user gives "first" to my program, I have to give him 0x1234. So my doubt
    is, how can I parse text files in C++.

    And in text file, I have to serach in /start and /end block.
    Thanks in Adv
     
    Imran, Aug 11, 2004
    #1
    1. Advertising

  2. "Imran" <> schrieb im Newsbeitrag
    news:cfckk8$kse$...
    >
    >
    > hello all, I have to parse a text file and get some value in that.
    >
    > text file content is as follows.
    >
    > ####TEXT FILE CONTENT STARTS HERE #####
    > /start
    > first
    > 0x1234
    > AC
    > /end
    >
    > /start
    > first
    > 0x12345
    > AC
    > /end
    >
    > /start
    > first
    > 0x12344
    > AC
    > /end
    >
    > someotherdatahere
    > ####TEXT FILE CONTENT ENDSHERE #####
    >
    > If user gives "first" to my program, I have to give him 0x1234. So

    my doubt
    > is, how can I parse text files in C++.
    >
    > And in text file, I have to serach in /start and /end block.
    > Thanks in Adv


    ifstream is("filename.txt"); // open a file stream

    string line; // S string for a line to read
    while(is.good()) // As long as there's data
    {
    is >> line; // read a line
    cout << line << endl; // output it
    }
     
    Gernot Frisch, Aug 11, 2004
    #2
    1. Advertising

  3. Gernot Frisch wrote:
    >
    >
    > ifstream is("filename.txt"); // open a file stream
    >
    > string line; // S string for a line to read
    > while(is.good()) // As long as there's data
    > {
    > is >> line; // read a line
    > cout << line << endl; // output it
    > }


    Not a good idea. The typical question with code like this
    is: "Why is the last word processed twice?"

    A stream goes into a fail state (such as eof) only until
    you try AND fail to read past the end of file. Thus the
    above loop will have undefined behaviour when is >> line
    fails the first time (usually at eof). The read operation
    fails and yet you process it as if nothing has happened.

    So at least it has to read

    while( is.good() )
    {
    is >> line;
    if( is.good() )
    cout << line << endl;
    }

    The usual idiom in C++ is

    while( data can be read ) {
    do something with the read data
    }

    if( stream is not in eof state )
    there was an error during read
    else
    all data could be read correctly

    ------

    while( is >> line ) {
    cout << line << endl;
    }

    if( !is.eof() ) {
    cout << "There was an error during read\n";
    return;
    }



    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 11, 2004
    #3
  4. > while( is >> line ) {
    > cout << line << endl;
    > }
    >
    > if( !is.eof() ) {
    > cout << "There was an error during read\n";
    > return;
    > }


    Thank you, I didn't know. I use fopen or CreateFile in an own class.
    -Gernot
     
    Gernot Frisch, Aug 11, 2004
    #4
  5. Gernot Frisch wrote:
    >
    > > while( is >> line ) {
    > > cout << line << endl;
    > > }
    > >
    > > if( !is.eof() ) {
    > > cout << "There was an error during read\n";
    > > return;
    > > }

    >
    > Thank you, I didn't know. I use fopen or CreateFile in an own class.


    It's the same issue, C++ took over this behaviour from C.

    (In a nutshell: Neither C nor C++ try to guess what the next
    input operation will do. Only after that operation is done
    it is known if it failed. Note that this is eg. different
    to PASCAL, where eof becomes true after the last record
    from a file has been read. Thus in PASCAL programs you
    often see
    while( not eof() ) do begin
    read
    process
    end

    But C and C++ are different. eof becomes true only after
    an attempt to read past the end of file and not when the
    last data from the file has been read.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 11, 2004
    #5
  6. Karl Heinz Buchegger <> wrote in message news:<>...
    > while( is >> line ) {
    > cout << line << endl;
    > }
    >
    > if( !is.eof() ) {
    > cout << "There was an error during read\n";
    > return;
    > }


    Note that if the line in the file has some space or tab, it wouldn't
    be fully read into the line variable.

    Thats why I always use:
    while (getline(is, line))
    {
    //process the line read
    }

    if (!is.eof())
    {
    //the input wasn't fully read
    }

    Best regards,

    Marcelo Pinto.
     
    Marcelo Pinto, Aug 11, 2004
    #6
  7. Imran

    Jeff Flinn Guest

    "Imran" <> wrote in message
    news:cfckk8$kse$...
    >
    >
    > hello all, I have to parse a text file and get some value in that.


    ....

    > If user gives "first" to my program, I have to give him 0x1234. So my

    doubt
    > is, how can I parse text files in C++.


    See http://www.boost.org/libs/spirit/index.html

    Jeff F
     
    Jeff Flinn, Aug 11, 2004
    #7
  8. "Imran" <> wrote in message news:<cfckk8$kse$>...
    > hello all, I have to parse a text file and get some value in that.
    >
    > text file content is as follows.
    >
    > ####TEXT FILE CONTENT STARTS HERE #####
    > /start
    > first
    > 0x1234
    > AC
    > /end
    >
    > /start
    > first
    > 0x12345
    > AC
    > /end
    >
    > /start
    > first
    > 0x12344
    > AC
    > /end
    >
    > someotherdatahere
    > ####TEXT FILE CONTENT ENDSHERE #####
    >
    > If user gives "first" to my program, I have to give him 0x1234. So my doubt
    > is, how can I parse text files in C++.
    >
    > And in text file, I have to serach in /start and /end block.
    > Thanks in Adv


    To solve this problem I would use a state machine:

    </start>
    idle ---------> processing ---\
    ^ |
    | </end> |
    \----------------------------/

    The idle class does nothing to the input unless it encounters a /start
    when it transfers control to the processing class which is responsible
    for processing the input. When the processing class encounters a /end
    it transfers control back to the idle class. (read the GoF pattern
    that deals with state machines)

    Note that the processing class may be more than one class. Your
    "example file" suggest that it would be necessary to have four
    diferent classes to do the processing one for each line of your
    "register".

    Good luck,

    Marcelo Pinto.
     
    Marcelo Pinto, Aug 11, 2004
    #8
  9. Imran wrote:
    > hello all, I have to parse a text file and get some value in that.
    >
    > text file content is as follows.
    >
    > ####TEXT FILE CONTENT STARTS HERE #####
    > /start
    > first
    > 0x1234
    > AC
    > /end
    >
    > /start
    > first
    > 0x12345
    > AC
    > /end
    >
    > /start
    > first
    > 0x12344
    > AC
    > /end
    >
    > someotherdatahere
    > ####TEXT FILE CONTENT ENDSHERE #####
    >
    > If user gives "first" to my program, I have to give him 0x1234. So my doubt
    > is, how can I parse text files in C++.
    >
    > And in text file, I have to serach in /start and /end block.
    > Thanks in Adv
    >
    >


    How do you know which block to pull the information out of?
    Looks like a bad or poorly constructed data file.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Aug 12, 2004
    #9
    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. .Net Sports
    Replies:
    11
    Views:
    1,438
    .Net Sports
    Jan 17, 2006
  3. Naren
    Replies:
    0
    Views:
    584
    Naren
    May 11, 2004
  4. Kai Schlamp
    Replies:
    1
    Views:
    419
    Arne Vajhøj
    Mar 27, 2008
  5. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    454
    Domenico Discepola
    Sep 2, 2004
Loading...

Share This Page