Help parsing text file

This is a discussion on Help parsing text file within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm trying to parse a text file like this: Code: i:-15 # Comment Line 1 u:30 # Commnet ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Help parsing text file

    Hi all,

    I'm trying to parse a text file like this:
    Code:
    i:-15 # Comment Line 1
    
    u:30 # Commnet Line 2
    
    d:5/2009 # Comment Line 3
    
    $:154.72 # Comment Line 4
    where the first char is the type, the number or date is some primitive or object, and obviously everything follwoing the '#' is a comment

    I'm using this method right now:
    Code:
    parseFile( ifstream& if, Part& p, string fileName ){
    
      // ommitting the obvious stuff here...
    
      string temp;
      char type;
      while( getline( if, temp, ':' ){
    
        // Declare temp primitives and objects here:
        int tempInt;
        unsigned int tempUint;
        // etc...
    
        switch ( type ){
    
          case 'i' :
    
            getline( if, intString, '#' );
            intStream << intString;
            if( !( intStream >> tempInt ).fail() ){
    
              p.setInt( tempInt );
            }
    
             // etc
    Problem is, if there's more than one int, or whatever in Part p, it overwrites the tempInt or whatever ( respectively ).

    This needs to be a very general solution. I know I can't update some Part p within the function the way I have it right now. I just need a fresh idea or an extra set of eyeballs on how to do this.

    Any ideas are most appreciated.

    Thank you

    EDIT:

    for clarity, suppose Part looks like this:
    Code:
    class Part{
    
      public:
    
        // ...
    
      private:
    
       int firstInt;
       int secondInt;
    };
    case 'i' in the switch can't handle both of these like this. I need to somehow set things up so that the file parser can return a specific type.
    Last edited by dudeomanodude; 07-16-2008 at 08:19 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm confused. Are there two numbers on one line? Are there supposed to be two numbers on one line? Do you need to do different things depending on how many numbers there are? What, exactly, is going on?

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    A line in the text file has the following format:
    Code:
    type:value #comment
    So an example might look like:

    Code:
    i:5 # Widget initial quantity
    where 'i' is for 'int', '5' is the value, and everything after '#' is a comment


    Now, I attempted to use a switch statement based on the type 'char' ( 'i' in the above example ). The problem arises when there are more than one integers. Like:
    Code:
    i:5 # Widget initial quantity
    
    i:-15 # Widget Minimum Inventory Balance
    I just need help or a push in the right direction to restructure things a bit. Ultimately a general and low-complexity solution is a must.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    so you want to create vector (or list) of values read from the file? What should be the output?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you know what the numbers should be (without parsing the comments too)? Are they always in the same order?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    How do you know what the numbers should be (without parsing the comments too)? Are they always in the same order?
    Why not just read a whole line, strip off comments by removing anything beyond #, and then process the remainder of the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by matsp View Post
    Why not just read a whole line, strip off comments by removing anything beyond #, and then process the remainder of the string.

    --
    Mats
    Yea that's a lot better for complexity sake.

    >>so you want to create vector (or list) of values read from the file? What should be the output?

    Well they'll be loaded to a "Part" object for further processing.

    >>How do you know what the numbers should be (without parsing the comments too)? Are they always in the same order?

    You've just hit at the heart of it. For my current purposes, yes, I can assume the order will always be the same.

    However I need a more general way to do things where parts might not follow that order, i.e. they have other fields than the parts I currently deal with.

    So perhaps the part object needs to be redesigned, not just the parsing aspect of things.

    ???
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For current purposes:
    Then you can keep track, I suppose, of how many numbers you've read along with (if this is the first i, do this; the third i, do that; etc.)

    For future purposes:
    You're going to have to be able to distinguish them somehow; if you just have
    i:5
    i:-15
    in one file and
    i:-15
    i:5
    in another, I don't see any way to solve that problem. Different letters, maybe, or enforce an ordering, or ... something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Parsing specific data from one text file to another..
    By chops11 in forum C Programming
    Replies: 2
    Last Post: 09-13-2005, 10:50 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21