Thread: How can I get ifstream's getline function to include leading whitespaces?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    How can I get ifstream's getline function to include leading whitespaces?

    Hi All,

    I am trying to read in lines in a text file, and the leading whitespaces are important! However the getline function isn't reading in the leading whitespaces. Is this a normal way for getline to work? And is there a way around this?

    Thanks for the help!

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    My mistake: getline does NOT ignore leading whitespaces. I checked the binary tree I was reading chars into and it was the correct length. But, it doesn't PRINT spaces.

    I'll post the printing code involved (in order of function call):

    Code:
    const string mazeRow::toString() const
    {
       points.print();
       string s;
       return s;
    }
    points is a binNode<mazePoint> object, so this function is called:

    Code:
    template <>
    void binNode<mazePoint>::print() const
    {
       if (left != NULL) left->print();
       std::cout << nodeData.toString();
    	if (right != NULL) right->print();
    }
    And here is the toString function for the node...
    Code:
    const string mazePoint::toString() const
    {
       stringstream ss;
       string s;
       ss << point_type;
       ss >> s;
       return s;
    }
    Does string stream ignore whitespaces or something?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Correction: it ignores ALL whitespaces. Not just leading ones.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by abrownin View Post
    Correction: it ignores ALL whitespaces. Not just leading ones.
    You can use std::getline on the stringstream object (just as you did with cin), or you can simply assign the string to stringstream::str( ).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    I managed to solve the problem by replacing

    Code:
    stringstream ss;
       string s;
       ss << point_type;
       ss >> s;
       return s;
    With

    Code:
       cout << point_type;
       string s;
       return s;

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by abrownin View Post
    I managed to solve the problem by replacing

    Code:
    stringstream ss;
       string s;
       ss << point_type;
       ss >> s;
       return s;
    With

    Code:
       cout << point_type;
       string s;
       return s;
    That doesn't solve your problem, it just masks it. You aren't storing the data at all...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Good point! This worked:

    Code:
       string s;
       s = point_type;
       return s;
    I have a separate toString function which does the following:

    Code:
       points.print();
       string s;
       return s;
    This is calling a print() function which in turn calls the previous toString function. I've tried capturing the output but it doesn't work! Like this:

    Code:
       stringstream ss;
       string s;
       ss << points.print();
       ss >> s;
       return s;
    This is the compiler error I receive:

    Code:
    In member function 'const std::string mazeRow::toString() const':
    assign3.cpp:193: error: no match for 'operator<<' in 'ss << ((const mazeRow*)this)->mazeRow::points. bintree<dataType>::print [with dataType = mazePoint]()'
    Is there any way around this?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your print function returns no data, and accepts no data, so you can't do anything other than what it wants to do. If redirecting that output seems like a good idea, then you need to change the way your print function works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM