Thread: getline/substr command help

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Question getline/substr command help

    Can anyone give me an example on the getline/subst commands?

    Example:

    123UM1BRIAN
    123UH2BRIAN
    124ZZ1MOSES
    124UM2MOSES
    125BH1ISHMAEL

    I need to read a file like this and read each line and pull parts of each line to store into an array. The first 3 letters for the member#, 2 letters for the doctor area code, 1 letter for the transaction date and 10 letters for the member name.

    I can do this with a character for separation , but how do you break apart a string with no spaces or other characters? Any help would be appreciated.

    -ProLin

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    have you tried it by yourself??? I'll be you havent.

    Think about it.. if you know how many chars are for each thing, then you dont need a delimeter!!!

    Code:
    char buffer[3+2+1+10+1+1]; // extra one for null char
    char memNum[4];
    char areaCode[3];
    char transDate;
    char name[11];
    
    cin.getline(buffer, sizeof(buffer));
    
    strncpy(memNum, buffer, 3); // get the first three characters
    memNum[3] = 0; // terminate the string
    
    strncpy(areaCode, buffer + 3, 2); // next 2 chars
    areaCode[2] = 0; // terminate the string
    
    transDate = buffer[5];
    
    strncpy(name, buffer + 6, 10); // the name
    name[10] = 0; // terminate the string
    I didn't compile this code.. but read and learn from it and try and make it work. i'm sure you'll figure it out...

    next time, try and do it yourself first!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Yes, I have tried numerous times to use these getline/substr commands. I will try your method and let you know the results. I appreciate your help.

Popular pages Recent additions subscribe to a feed