   Reading the spaces in text files
  I am wondering if there is a way to read files into strings and keep the spaces.#include <iostream>#include <string>#include <fstream>using namespace std;int main( ) {string author, title, pubyear;ifstream Info;Info.open("data.txt");while(Info>>author&&Info>>title&&Info>>pubyear){cout<<"Author:  "<<author<<endl;cout<<"Title:   "<<title<<endl;cout<<"Pub Yr:  "<<pubyear<<"\n\n";}Info.close(); return 0;}And here is a portion of the text file, I wanna keep the spaces when I turn it to string.  Any Ideas?:Malcolm GladwellThe Tipping Point: How Little Things Can Make a Big Difference2002Barack ObamaDreams from My Father2004Malcolm GladwellBlink2007J. K. RowlingHarry Potter and the Deathly Hallows2007Markus ZusakThe Book Theif2007David GrannThe Lost City of Z2009   
  If you want to read a line at time you can use std:getline().   ;  It worked perfectly, thanks!Now how do I cin a string with a space?   string search;   cout<<"Type what you want to search:  ";cin>>search;cout<<"\n"<<search<<endl;I want to make search equal to "David Grann" or "The Lost City of Z"While at the interface.[Edited by - Twili on April 24, 2009 10:08:29 PM];Quote:Original post by TwiliIt worked perfectly, thanks!Now how do I cin a string with a space?The same way.You seem to have a problem in your understanding. "cin" is not a verb; it's a noun. You don't cin the string; you read a string from cin. Since std::cin is an istream, it is a valid argument for std::getline() just like any ifstream instance is.   ;  Can you show me what this would look like?I declared achar search[256];and put:std::cin.getline (search,256) ;in the proper places, but now during the interface it just skips the part where I enter in the name.   ;  If you use the extraction operator on a stream before calling getline() on the stream there may be a stray newline in the buffer that you need to flush before calling getline(). In any case, you should probably consider using the non-member function std::getline() instead of the member function getline() so that you can read into a std::string object.   ;Quote:Original post by TwiliCan you show me what this would look like?I declared achar search[256];and put:std::cin.getline (search,256) ;Quote:Original post by SiCraneIf you want to read a line at time you can use std:getline().Not the getline member function of std::cin. The free function 'getline'.In C++, we use std::string to represent text. The free function std::getline reads into a std::string. You want to use this.std::string search;getline(std::cin, search);Voila. No length limit.Quote:in the proper places, but now during the interface it just skips the part where I enter in the name.If you read something with operator>>, it does not consume any trailing whitespace. A newline character is whitespace, so it will be left alone. Both getline versions read from the current position, until the first newline (actually, you can specify another character as the delimiter instead, but we'll ignore that for now). If the current position has a newline, then that means the read stops immediately, reading a line of zero length. A line of zero length is a perfectly valid (and often very useful) concept.To avoid this, the easiest approach in general (which is also quite robust) is to always use std::getline to read from the stream, and then use a std::stringstream instance to re-parse lines that are supposed to contain non-string data. This is also a good idea when using std::cin, because the console will only make data available to your program a line at a time anyway (your program is effectively paused until the user hits return, unlike what happens with a file).By the way, there is normally no need to close file streams explicitly, and you can use the constructor to open them instead.int number_of_instruments;std::string instrument_type;std::string location;/* Our file looks like:76 trombonesin the big parade.*/ifstream lyrics('76_trombones_lyrics.txt');std::string temp_line;std::getline(lyrics, temp_line);std::stringstream parser(temp_line);// We could use std::getline again for the instrument_type, if we want to// be able to read more than one word. But then we have to account for the// whitespace between the number and the instrument. You can do that by// reading into std::ws (which is a little magic thing that just eats the// whitespace).parser >> number_of_instruments >> instrument_type;std::getline(lyrics, location);;  Thanks for the help everyone, I got the code working the way I want it to now.   