Ignoring spaces...

This is a discussion on Ignoring spaces... within the C++ Programming forums, part of the General Programming Boards category; i need to ignore spaces before lines while reading them from a file...the problem is i don't know how many ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Ignoring spaces...

    i need to ignore spaces before lines while reading them from a file...the problem is i don't know how many spaces i could get...file could be like this:
    Code:
    hello
       hello
         hello
               hello
      hello
    hello
    i would rather not do this by first using getline and then getting substrings etc...
    can i use ignore effectively here so that when i get the next word from the stream for each line i get hello...

    Regards,

    Farooq

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    You can use formatted input, which will skip white space for you by default:
    Code:
    std::string str;
    cin >> str;
    gg

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    i'm reading from a text file...i know that in >> would read the first word without spaces...i guess that would be sufficient for my problem...

    what if i use getline with a delim as ',' implying:
    Code:
    getline(in, somestring, ',')
    this is in the case if i have a file like this:
    Code:
     2,3 hello
        3,4 hello
               5,6 hello
    1,4 hello
    in this case will getline return 2 3 5 1 respectively or would it return these with the amount of spacing before them?

    Regards,

    Farooq

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    getline will include whitespace, including newlines if ',' is your terminator. Thus it will read " 2" "3 hello\n 3" etc...

    If you just wish to trim leading whitespace you can use a manipulator
    Code:
    std::getline(is>>std::ws,line);
    ws acts like a dummy variable that accepts only whitespace, the result of >> applied to a stream is also a stream so we pass that to getline. This is also common when we need to mix parsing formated operations (that leave newlines) and getlines.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    I had a program awhile ago where I had to convert a string to upper case as well as remove special chars (such as '&')

    If you know all your input is going to be an alphabetic character you could consider doing what I did with isalpha();

    Code:
    int y=0;
    for(int i=0; i<(strlen(input)); i++) {
    
    
        if ( isalpha(input[i]) ) {              // remove
            message[y] = input[i];        // spaces and
            y++;                                    // special chars
        }    
                                        
    } // end for loop
    What this does is moves the input into a new array which only contains the needed characters. If it is not an alphabetic character, it is not copied.
    For spaces you could simply change the if statement to: "if input[i] is a space"

    Note: If the message array is shorter than the input array you will have to insert a null character at the end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 11:51 PM
  2. Ignoring Lines and Continuing through Spaces,
    By Blurr in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2007, 09:31 AM
  3. Help ignoring spaces.
    By inmaterichard in forum C Programming
    Replies: 1
    Last Post: 07-25-2007, 08:00 PM
  4. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 10:53 AM
  5. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 01:42 PM

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