why can't ifstream read spaces or enters

This is a discussion on why can't ifstream read spaces or enters within the C++ Programming forums, part of the General Programming Boards category; see here http://www.cprogramming.com/tutorial/lesson10.html
it stops reading after it reaches a space. Why is this and is there any easy way ...

Input from istreams is delimited by a space, this is why you can say something like:

cin>> i >> j >> k;

And use input with spaces:

10 20 30

Of course this becomes a problem when you want to read a string of data that includes spaces. In this case you want to have input delimited by a newline character, which is what the getline method does:

cin.getline ( array, SIZE );

or with C++ strings:

getline ( cin, someString );

Note that wherever I used cin, you could replace it with a user defined istream:

Also note that getline is defined in <string>, not <iostream> and the previous statement would cause an error if you don't include string. The getline from iostream is bound to cin and is used differently, so the using statement would be

using std::cin;

And the call to getline would be

cin.getline ( cstring, size, delimiter );

>at the very top of your code, just below your includes, if it is not
>already there (which it ought to be anyway)
Actually, according to good style it shouldn't be there. using namespace std; opens the entire std namespace to the program, thus making everything in it global, which is a bad thing. The preferred method is to either use the std:: prefix for anything in std or open only the parts of std that you will be using in the function that you will use them. If something is to be used in every area of the program then you can make it open globally, such as cout.

#include <iostream>
using std::cout;

This is a valid use of the using statement in the global scope. Anything more should require a great deal of thought and understanding of the consequences.

#include <fstream>
#include <iostream>
/* Edits: The .h extension is not the current standard */
int main()
{
char astr[10]; /* Edits: Identifiers starting with str are reserved */
//Used later
std::ofstream a_file("example.txt");
//Creates an instance of ofstream, and opens example.txt
a_file<<"This text will now be inside of example.txt";
//Outputs to example.txt through a_file
a_file.close();
//Closes up the file
std::ifstream b_file("example.txt");
//Opens for reading the file
b_file.getline ( astr, 10 );
//Reads one string from the file
std::cout<<astr;
//Should output 'this'
b_file.close();
//Do not forget this!
}
/* Big Edits: Namespaces are used to conform with the current standard,
no changes were made to handle errors, one should be wary of that
when working with files
*/

And here is the corrected tutorial with C++ strings:

Code:

#include <fstream>
#include <iostream>
#include <string>
/* Edits: The .h extension is not the current standard */
int main()
{
std::string astr; /* Edits: Identifiers starting with str are reserved */
//Used later
std::ofstream a_file("example.txt");
//Creates an instance of ofstream, and opens example.txt
a_file<<"This text will now be inside of example.txt";
//Outputs to example.txt through a_file
a_file.close();
//Closes up the file
std::ifstream b_file("example.txt");
//Opens for reading the file
std::getline ( b_file, astr );
//Reads one string from the file
std::cout<<astr;
//Should output 'this'
b_file.close();
//Do not forget this!
}
/* Big Edits: Namespaces are used to conform with the current standard,
no changes were made to handle errors, one should be wary of that
when working with files
*/