Thread: Yet another read file question ...

  1. #1
    Unregistered
    Guest

    Yet another read file question ...

    I have a text file that looks a little something like this:

    1;pete;shipping
    2;tom;shipping
    3;john;receiving

    My program has three list boxes, basically I want the program to read each line of the file, and split up each line using the ; token into 3 variables and put them into the list boxes. I know how to put them into the boxes, I just don't know what kind of approach I should use to read the lines, and split them up.

    I've grown so used to using actual databases like SQL (database isn't an option here) that I don't really have any idea how to tackle this.

    It seems easy enough but it's probably not, anyone have any examples, tips, or links they can pass on?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    heck why use a strtok ....

    instead just use the string class or the CString if you are using MFC....

    string sline

    read the line in.....I assume you know how...

    Code:
    CString s[10];
    int pos;
    int i = 0;
    
    while( 1 )
    
    {  
    
           int pos = sline.Find(";");
    
           if( pos >= 0)
            {
                  s[i] = sline.Left(pos + 1);
                  s[i].TrimRight(";"); 
                  sline.Delete( 0, s[i].GetLenth() );
                  i++;
            }
            else
                  break;
    }
    zMan

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Create a struc to hold the data.

    Open the file, find the size with GetFileSize().

    GlobalAlloc() for the whole file and read the whole file in one read to the buffer. Close the file.

    Alloc for the first record (sizeof(STRUCT))

    Copy into a temp record.

    Code:
    STRUCT      TempRecord,*RecordArray=NULL;
    char        *pStart=NULL,*pString=NULL;
    int         iRecordsRead=0;
    HGLOBAL     hMemHandle=NULL;
    
    pStart=sFile;//set to the start of the buffer
    while(*pStart!='\0')
    {
       TempRecord.iNumber=atoi(pStart);
       while(*pStart++ != ';');//move thru the ;
       pString=TempRecord.sDescription;
       while((*pStart != '\n')&&(*pStart !='\r')&&(*pStart != '\0')&&(*pStart != ';'))
            *pString++ = *pStart++;
       *pString='\0';
       while(*pStart++ != ';');
    //ect I think you get the idea
       while((*pStart == '\n')||(*pStart == '\r'))
    	pStart++;//move past the end of line
       CopyMemory(&(RecordArray[iRecordsRead]),&TempRecord,sizeof(STRUCT));
       iRecordsRead++;
       hMemHandle=GlobalHandle(RecordArray);
       hMemHandle=GlobalReAlloc(hMemHandle,( sizeof(STRUCT) * (iRecordsRead+1)), GMEM_MOVEABLE);
       //error check for mem alloc fail if(hMemHandle==NULL)
    }
    Free the files buffer with GlobalFree() and GlobalHandle() if needed.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 21
    Last Post: 06-16-2008, 02:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM