0

I have next code:

#include "boost/filesystem.hpp"
#include "boost/filesystem/fstream.hpp"
#include <iostream>
namespace fs = boost::filesystem;
...
fs::path p{ "mysuperfile.txt" };
fs::ifstream ifs{ p };
std::string data;
ifs >> data;
ifs.close();
...

And the problem is, that it reads content of file until first white space, which is only one first word. Nothing more. So the question is how is possible to read content of the file with white spaces, line breaks, tabs and all other possible characters with boost::filesystem:ifstream? thanks.

Payalord
  • 463
  • 2
  • 5
  • 16
  • I would assume the same way(s) as with standard library. Why are you using boost.filesystem just for this, anyway? – LogicStuff Apr 03 '18 at 10:42
  • No, not exactly the same. There is differences. I'm using boost because of boost functionality and I'm not sure that std is cross platform or not. Plus boost is already included. But that doesn't mean that I can't go with std. Just wanted to know is it possible to do this with boost::filesystem::ifstream which is already available. – Payalord Apr 03 '18 at 10:58
  • The `std` namespace the *standard library* namespace. Any conforming implementation of C++ needs to have it, or it won't really be C++. It is part of the C++ specification. If you don't know about it, then please [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Apr 03 '18 at 10:59
  • Good to hear that. But then why boost have it's own fstream? – Payalord Apr 03 '18 at 11:01

1 Answers1

1

Why not simply

std::ifstream ifs("mysuperfile.txt");
std::string data(std::istreambuf_iterator<char>(ifs),
                 std::istreambuf_iterator<char>());

After that, the contents of data should be an exact copy of the contents of the file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If I will not find solution with boost::filesystem::ifstream for sure I will use this one, thanks for your help. – Payalord Apr 03 '18 at 10:59
  • The problem with mixing boost and std is `std::ifstream` does not directly accept `boost:filesystem:path` instance. At least clang++ complains `no matching constructor for initialization of 'std::ifstream' (aka 'basic_ifstream')`. This should not be the case with recent `std::filesystem`. But it requires `c++ 17` – Kaan E. Mar 19 '19 at 23:17