Input from file no white space characters

This is a discussion on Input from file no white space characters within the C++ Programming forums, part of the General Programming Boards category; I'm reading data in from a file and putting it into a string then appending it to another file. The ...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Input from file no white space characters

    I'm reading data in from a file and putting it into a string then appending it to another file. The problem with this is that all the data is put onto one line with no whitespace characters put into outputted file. I assume this is due to the string. Is there any way to be able to output whitespace characters?

    Here is part of the code:
    Code:
    CInput.open(FileName.c_str(), ios::in);
    		
    while (!CInput.eof())	//Read until end of file
    {
    	CInput >> FileLine;
    	FileData.append(FileLine);
    }
    Thanks,

    ...Dan
    Last edited by Dan17; 05-08-2006 at 07:28 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I'm going to guess what you're asking since I didn't find your explaination too clear.

    You want to read in the file, and you want to append to a string exactly, including the whitespace that is consumed by the istream::operator>>(). This is quite simple, unfortunately, using the operator>>, you give yourself a problem. That operator will stop on ANY whitespace. Which means you'd never know exactly what it's stopping on unless ofcourse you used istream::peek(). Better yet, you should probably read in a whole line with the getline() function. This way you can declare a delimeter to stop on, and append it after each encounter.
    Code:
    CInput.open(FileName.c_str(), ios::in);
    		
    while (!CInput)	// Better not to use eof()
    {
    	getline(CInput, FileLine, '\n');
    	FileData += FileLine + '\n';  // The line plus the consumed delimeter
    }
    Sent from my iPadŽ

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Yeah that's exactly what I meant. Thanks for the help.

    ...Dan

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > while (!CInput) // Better not to use eof()
    Isn't actually any better either.

    Code:
    while (getline(CInput, FileLine, '\n'))
    {
    	FileData += FileLine + '\n';  // The line plus the consumed delimeter
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Isn't actually any better either.
    It's a little better. Last time I checked, controlling the loop with eof() works when you are using getline. The only bad part is that it doesn't check for stream errors. Switching to while (!CInput) does that.

    I do agree that your version is the best, though, even if only for consistency.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by Salem
    > while (!CInput) // Better not to use eof()
    Isn't actually any better either.
    I agree with you. Had it been an iostream function, I would have controlled the loop with it, but the truth is I wasn't sure of the return on the std::getline() function. So I figured better safe the sorry.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM

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