Code:char text[50]; ifstream a_file("test.txt"); a_file >> text; a_file.close(); cout << text << endl;
test.txt
Code:here is a sentence
outputs to the screen
"here"
Code:char text[50]; ifstream a_file("test.txt"); a_file >> text; a_file.close(); cout << text << endl;
test.txt
Code:here is a sentence
outputs to the screen
"here"
The operator>> stops at the first whitespace... use getline. There are plenty of threads about how to use getline, including one only a few notches below this one with the same question.
i tried this
it compiles but does not print anything when running.Code:ifstream a_file("test.txt"); a_file.getline(text, 50); a_file.close();
try this
Code:#include <iostream> #include <fstream> using namespace std; int main() { char text[64]; ifstream a_file("test.txt"); a_file.getline(text,64); cout << text << endl; return 0; }