Search
 
SCRIPT & CODE EXAMPLE
 

CPP

get std string from file

#include <iostream>
#include <fstream>
#include <sstream>

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;

string readFileIntoString(const string& path) {
    ifstream input_file(path);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
             << path << "'" << endl;
        exit(EXIT_FAILURE);
    }
    return string((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
}

int main()
{
    string filename("input.txt");
    string file_contents;

    file_contents = readFileIntoString(filename);
    cout << file_contents << endl;

    exit(EXIT_SUCCESS);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: clear previous terminal output c++ 
Cpp :: google test assert stdout 
Cpp :: Integer Moves codeforces solution 
Cpp :: how to convert hexadecimal to decimal in c++ 
Cpp :: C++ linked list iterator 
Cpp :: c++ class 
Cpp :: print reverse number 
Cpp :: c++ little endian or big endian 
Cpp :: cpp vscode multipe compilation 
Cpp :: what is the default include path in ubuntu c++ 
Cpp :: iterate const vector 
Cpp :: how to use for c++ 
Cpp :: Basic Input / Output in C++ 
Cpp :: how to declare an enum variable c++ 
Cpp :: how to find product of a given numbers in c++ 
Cpp :: find nth fibonacci number 
Cpp :: cast cpp 
Cpp :: delete c++ 
Cpp :: custom slider cpt wordpress theme 
Cpp :: definition of singly linkedlist 
Cpp :: ? in cpp 
Cpp :: initialisation of a c++ variable 
Cpp :: c++ allocate dynamic with initial values 
Cpp :: c++ overloading by ref-qualifiers 
Cpp :: what is a .h file in c++ 
Cpp :: Madiar loh 
Cpp :: c++ round number to 2 decimal places 
Cpp :: log base 10 c+_+ 
Cpp :: C++ float and double simple example 
Cpp :: Types of Triangles Based on Angles in c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =