I want to read file and cout the way it looks within the file
Hello World! 11 001
34 040 33
22
what it is doing is cout like "Hello World! 11 001 34 040 33 22"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ifstream inFile;
string file data;
inFile.open("test.txt", ios::in);
cout << "File contents = " ;
while ( !inFile.eof() ) {
inFile >> data ;
cout << data;
cout << " ";
if ( data == "\n" ) // this has to be the error... maybe data needs to be converted to char... not sure
cout << "\n";
}
also I want keep the functionality to check "data" and compare it to keywords
What is happening is a common affliction. You are misunderstanding how STL streams work.
The >> operator is for parsing input data into numbers, etc. It always delineates on whitespace (which is space, tab, vertical tab, linefeed, carriage return). Basically it is a friendly replacement for what C people use scanf().
If you want to track where the line ends, you can peek() for '\n':
or you can just read everything one line at a time:
1 2 3
string data;
while (getline( inFile, data ))
cout << data << '\n';
Notice that in the first, you read each datum (whitespace-separated string) one at a time. In the second, you read an entire line (which may contain zero or more whitespace-separated strings) at a time.