Yet another read file question ...

This is a discussion on Yet another read file question ... within the Windows Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #1

    Yet another read file question ...

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


    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
    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...

    CString s[10];
    int pos;
    int i = 0;
    while( 1 )
           int pos = sline.Find(";");
           if( pos >= 0)
                  s[i] = sline.Left(pos + 1);
                  sline.Delete( 0, s[i].GetLenth() );

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    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.

    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++ != ';');//move thru the ;
       while((*pStart != '\n')&&(*pStart !='\r')&&(*pStart != '\0')&&(*pStart != ';'))
            *pString++ = *pStart++;
       while(*pStart++ != ';');
    //ect I think you get the idea
       while((*pStart == '\n')||(*pStart == '\r'))
    	pStart++;//move past the end of line
       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-22-2008, 12:19 AM
  2. Replies: 21
    Last Post: 06-16-2008, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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