Jake Wickham Jake Wickham - 1 month ago 16
C++ Question

std::getline() returning memory address when using cout

When the cout statement executes it returns a memory address.

string data = "Test String ";
string info;
ofstream file("data.dat", ios::binary);
ifstream fileI("data.dat", ios::binary);
file << data;
file.close();
cout << getline(fileI, data); // returns a memory address
return 0;


Why is this happening?

Answer

The getline function itself returns the input stream from the given first argument (fileI in your code). The result is saved in the 2rd argument (data in your code). So you need to run getline(fileI, data)first and then output the string data.

string data = "Test String ";
string info;
ofstream file("data.dat", ios::binary);
ifstream fileI("data.dat", ios::binary);
file << data;
file.close();
getline(fileI, data);
cout << data;
return 0;

Also see the documentation of getline