How to Read File into String in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to create an input file stream for reading from the specified file with a combination of std::getline to extract the lines from the file content that can be concatenated and stored in a string variable. ApproachOpen the file using std::ifstream file(filePath).Use the is_open() method to check if the file was opened successfully, if not print error message and return.Use a loop with std::getline(file, line) to read each line of the file into a string and print the populated string.Close the file stream.C++ Program to Read a File into StringThe below program demonstrates how we can read the content of a file into a std::string line by line in C++. C++ // C++ Program to demonstrate how to Read a File into String #include <fstream> #include <iostream> #include <string> using namespace std; int main() { // get the filepath string filePath = "myFile.txt"; // Open the file using ifstream ifstream file(filePath); // confirm file opening if (!file.is_open()) { // print error message and return cerr << "Failed to open file: " << filePath << endl; return 1; } // Read the file line by line into a string string line; while (getline(file, line)) { cout << line << endl; } // Close the file file.close(); return 0; } Output File Content: Hi, Geek! Welcome to gfg. Happy Coding ;)Time Complexity: O(n), here n is total number of characters in the file.Auxiliary Space: O(n) Create Quiz Comment S sonijaiog3d Follow 0 Improve S sonijaiog3d Follow 0 Improve Article Tags : C++ Programs C++ cpp-file-handling C++ File Programs CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like