0

I have a file named "lio.txt" with text, "my name is bio". I want to shift the full line to a string variable. How can I do that?

  • Does this answer your question? [How do I read an entire file into a std::string in C++?](https://stackoverflow.com/questions/116038/how-do-i-read-an-entire-file-into-a-stdstring-in-c) By the way, it took me few seconds to search on google and find it. You should do a proper research **before** asking a question. But we can't help you any further with your specific issue since you didn't show your attempt (if any). – Fareanor Nov 16 '21 at 12:20

1 Answers1

1

this code should do it (error checking is ommited):

std::string read_all_file()
{
    std::ifstream file{ "file.txt", std::ios::ate }; // open the file and set the get position to the end
    std::string buff;
    buff.resize(file.tellg()); // the get position is the file size
    file.seekg(0, file.beg); // seek to the begin of the file to start reading
    file.read(buff.data(), buff.size()); // read the whole file in buff
    return buff;
}
dev65
  • 1,440
  • 10
  • 25