Thread: White space problems in file input

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

    Unhappy White space problems in file input

    Hi,

    I am currently using the

    fin.getline(buffer,30, ':' );

    to read in part of a file upto a colon, where fin is the input file .

    However I want to ignore white spaces so that if the input was

    h ello:

    it would read it in as hello.

    I am a bit stuck and was hopin that someone could help with this problem . Thank you in Advance

    Craig Smith

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Code:
    int i=0;
    while (!buffer.eof())
    {
       buffer.get(character);
       if (character != ' ')
       {
           buffer.put(char_array[i]);
           ++i;
       }
    } // end while
    
    char_array[i] = '\0';
    
    cout << char_array;
    Blue

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    or step through your array using getline to load your array and place characters into a second array if not a space...
    Blue

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    I was hoping that there was a simpler way of doing that because I am splitting up a big file into 6 sections eg:

    while (! fin.eof() )
    {

    fin.getline(buffer,30, ':' );
    fin.getline(firstTeam,30, '(' );
    fin.getline(firstScore,5, ')' );
    fin.getline(secondTeam,30, '(' );
    fin.getline(secondScore,5, ')' );
    fin.getline(buffer,30, '\n' );


    and I want each one to ignore white spaces .

    Can anyone help ???

    Thank you
    Craig

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Nope, reading file one char at a time, or parsing line one char at a time is about the only way I know of, unless the data is space limited in which case you can use >> to eliminate all whitespace. From what you describe a parsing function to eliminate whitespace from a line read in by getline and delimited by the various chars as posted in your example sounds the most reasonable to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM