Thread: white spaces

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    32

    white spaces

    Just something I dont understand...Lets say you had the follwing data in a file:

    cat and mouse
    dog and flea
    123

    fish and bait
    bird and worm
    456

    etc....

    if I read in the file as

    Code:
    while (in.getline (str1, MAX,'\n'))
    {
        in >> ws;
        in.getline( str2, MAX,'\n');
        in >> ws >> id >> ws >> ws;
    }
    it doesnt work. But if I write the last line as
    Code:
    in >> ws >> id >> ws;
    everything is fine. Why is that? There are 2 enter's before the next block of data.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is std::getline().

    std::string sInput;

    while (std::getline(cin, sInput))
    {}

    Kuphryn

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I believe ws takes in all the whitespaces before the next character/until EOF, so therefore, after the first ws executes, there's nothing left for teh second ws to get rid of...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading white spaces? oh and scanf_s
    By RobotGymnast in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2008, 05:32 AM
  2. white spaces ...
    By eagles in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2006, 07:55 AM
  3. Replies: 1
    Last Post: 03-08-2005, 12:02 PM
  4. string input...with white spaces?
    By Pureghetto in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 01:52 PM
  5. Reading in text file into an array, white spaces...
    By error in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2003, 09:39 AM