Thread: reading values from a line of text

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    reading values from a line of text

    I've got a line of txt
    EHRAUFE10235869496XXX 0938593863005003002049964006

    and I have to read in only certain pieces of it.
    this is what I've got so far.
    int main()
    {
    char engine[5];
    char major[4];
    char minor[4];

    // open file

    infile.ignore(6,'\n'); // ignores the first 6 spaces or finds \n
    infile.getline(engine,5); // asigns the next 5 to engine

    /* so far it works fine, now I'm trying to get to the other side of the space to pick-up the major and minor cycles.*/
    infile.ignore(14, '\n');
    infile.getline(major,4);
    // this one will not pick-up anything ?
    any input would be great , maybe substr(pos, len) but I can't hget it to work at all.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    As far as I know getline will read from the current line, then set focus to the next line.

    Your best bet would be to read the entire string in and then parse it. The easiest way I can see is using the CString class.

    Then again, I might be totally wrong. If so, feel free to
    infile.ignore me.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If the comments in your code are correct, then your code should look like this:
    Code:
    int main()
    { 
    char engine[6];
    char major[5]; 
    char minor[5];
    
    // open file
    
    infile.ignore(6,'\n'); // ignores the first 6 spaces or finds \n 
    infile.getline(engine,6); // assigns the next 5 to engine
    
    /* so far it works fine, now I'm trying to get to the other side of
    the space to pick-up the major and minor cycles.*/ 
    infile.ignore(14, '\n'); 
    infile.getline(major,5);
    Some comments. First to store strings, character arrays should be one bigger than the size of the string (room for the string terminator). So:
    char engine[6]; //Holds five characters plus terminator
    char major[5]; //Holds four characters plus terminator
    char minor[5]; //Holds four characters plus terminator

    Each getline should match with the size declared.
    infile.getline(engine,6); // assigns the next 5 to engine
    infile.getline(major,5);
    Last edited by swoopy; 03-26-2002 at 05:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  2. Reading Text files
    By AmazingRando in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2003, 08:30 PM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 04:39 AM