1

From the documentation the ifstream has an inherited member function from istream >> which sets the noskipws to true. Furthermore I created the ifstream object with the argument of std::ios::binary, so that's even more reason I expected it to read whitespaces. The result I get is:

std::ifstream inFile(file, std::ios::binary);
if (!inFile.is_open() || !inFile.good()) {COUT "Error opening file\n"; return false; }

inFile >> script;  // Here, it is skipping whitespaces, script is an std::string

I noticed in the documentation there is another function >> operator which is std::operator >>. In this one it states that noskipws is set to false. My question is, if inFile is an ifstream object and I used >>, shouldn't the noskipws be set to true and therefore be reading the whitespaces?

How can I read the file as a binary which includes everything?

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • Possible duplicate of [Why can't I read fstream's binary data with operator>>?](https://stackoverflow.com/questions/4151840/why-cant-i-read-fstreams-binary-data-with-operator) – LogicStuff Aug 31 '17 at 09:48
  • Use `std::istreambuf_iterator` instead. – LogicStuff Aug 31 '17 at 09:49
  • @LogicStuff By using inFile.read() does that mean I have to count the size of the buffer? It expects a size argument. Also, I wanted to read it into a string, does that mean I need to read it into a char buffer, then construct a string object from the char buffer? – Zebrafish Aug 31 '17 at 09:55
  • You have to call `std::string::resize` and `inFile.read(&str.front(), str.size());` (`str.data()` in C++ 11). – LogicStuff Aug 31 '17 at 10:00
  • @LogicStuff Let's say I resize the string to 100, and call inFile.read(&str.front(), str.size()), does that mean it will try to read 100 bytes from the file even though the file may only be 50 bytes? That's why I asked do I have to find out the size of the file first in bytes before I do this? I didn't know taking the &address of an iterator was the same value as the raw pointer. Edit: front() returns a reference, not an iterator. – Zebrafish Aug 31 '17 at 10:06
  • See the [reference](http://en.cppreference.com/w/cpp/io/basic_istream/read). But you should really use [this](https://stackoverflow.com/a/6532289/3552770) approach. – LogicStuff Aug 31 '17 at 10:10

0 Answers0