Table of Contents

C - C++ Files - Read file into string

read_file_into_string.hpp

#pragma once
 
#include <string>
 
std::wstring read_file_into_wstring(std::string filename);
std::string read_file_into_string(std::string filename);

read_file_into_string.cpp

#include "read_file_into_string.hpp"
#include <sstream>
#include <fstream>
 
std::wstring read_file_into_wstring(std::string filename)
{
  std::wostringstream stream;
  stream << std::wifstream(filename).rdbuf();
 
  return stream.str();
}
 
 
std::string read_file_into_string(std::string filename)
{
  std::ostringstream stream;
  stream << std::ifstream(filename).rdbuf();
 
  return stream.str();
}

References

https://en.cppreference.com/w/cpp/io/basic_ios/rdbuf