Store text file in a variable | c++ [duplicate]

  assertion, c++, fstream, string, txt

I need to store all data from .txt file in string variable. Here is the code I wrote, but there occurs assertion error when I try to cout that variable. Can anyone help?

fstream myPh("phonebook1.txt"|ios::in);
string str((istreambuf_iterator<char>(myPh)),istreambuf_iterator<char>());
for(int i=0;i<30;i++)
    cout<<str[i];

Source: Windows Questions C++

LEAVE A COMMENT