This works fine:
stream = OpenRead["ExampleData/strings"]; ReadString[stream, EndOfFile] (* "Here is text. And more text. " *) Close[stream] (* "ExampleData/strings" *)
But if I use
EndOfBuffer instead of
EndOfFile, Mathematica issues an error message (despite returning the expected result).
stream = OpenRead["ExampleData/strings"]; ReadString[stream, EndOfBuffer] During evaluation of BinaryReadList::bfmt: The stream InputStream[Name: strings, Unique ID: 5] has been opened with BinaryFormat -> False and cannot be used with binary data. (* "Here is text. And more text. " *) Close[stream] (* "ExampleData/strings" *)
Why does this happen? Is this a bug? This error is normally shown when binary read functions (such as
BinaryRead) are used on streams that have
BinaryFormat -> False. But
ReadString is not such a function. It is designed to read textual data, with newlines interpreted.
Why am I trying to use
EndOfBuffer? I am looking for a solution for reading form Mathematica streams in C++ code. One piece of the puzzle is reading only a limited amount of data (that fits into memory) with good performance. I thought that instead of trying to explicitly control the size of the data returned, perhaps I should read as much as Mathematica itself keeps in its buffer. Then running out of memory shouldn't be a problem.