#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int main()
{
const char* path = "C:\Dev\devAstroides\printFileToScreen\Hello.txt";
std::string Code;
std::ifstream File;
File.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
File.open(path);
std::stringstream Stream;
// read file's buffer contents into streams
Stream << File.rdbuf();
// close file handlers
File.close();
// convert stream into string
Code = Stream.str();
}
catch (std::ifstream::failure & e)
{
std::cout << "ERROR::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
std::cout << Code.c_str();
}
This is supposed to open a text file and print its content to the console. But it doesn't work. The error message is always triggered and it doesn't print the file!
I also wonder how one can replace the full file-path with a relative one, so it works on other computers, or if the project is moved.