#include<iostream>
#include<fstream>
usingnamespace std;
int main()
{
string infile;
ifstream infile;
infile.open("test.txt");
data.c_str();
infile>>data;
cout << data << endl;
// read the data from the file and display it.
infile >> data;
cout << data << endl;
// close the opened file.
infile.close();
return 0;
}
named the same thing, which you can't do, you'll have to name one of them something different. I assume you had the string named data based on what you have here: data.c_str(); but calling c_str(); without assigning it to anything wouldn't make sense as it returns a pointer to an array containing data of the string ( http://www.cplusplus.com/reference/string/string/c_str/ ). Also you don't even need to call c_str(); anywhere here because you aren't doing anything that would require a c string.
Also you need to #include <string> and then what you have should work, just note that infile>>data; will read only one word at a time