Thread: Help parsing text file

Threaded View

Previous Post Previous Post   Next Post Next Post
  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)

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, 03:58 PM