What's the difference between read, readsome, get, and getline?
c++
Solution
`get` and `getline` are quite similar, when `get` is called with parameters `( char_type* s, std::streamsize count )`. However, `get` reads from the stream until a delimiter is found, and then leaves it there. `getline` by comparison will pull the delimiter off the stream, but then drop it. It won't be added to the buffer it fills.
`get` looks for `\n`, and when a specific number of characters is provided in an argument (say, `count`) it will read up to `count - 1` characters before stopping. `read` will pull in all `count` of them.
You could envisage `read` as being an appropriate action on a binary datasource, reading a specific number of bytes. `get` would be more appropriate on a text stream, when you're reading into a string that you'd like null-terminated, and where things like newlines have useful syntactic meanings splitting up text.
`readsome` only returns characters that are immediately available in the underlying buffer, something which is a bit nebulous and implementation specific. This probably includes characters returned to the stream using `putback`, for example. The fact that you can't see the difference between `read` and `readsome` just shows that the two might share an implementation on the particular stream type and library you are using.
Problem
What is the difference between these functions. When I use them they all do the same thing. For example all three calls return `"hello"`: ``` #include <iostream> #include <sstream> int main() { stringstream ss("hello"); char x[10] = {0}; ss.read(x, sizeof(x)); // #1 std::cout << x << std::endl; ss.clear(); ss.seekg(0, ss.beg); ss.readsome(x, sizeof(x)); // #2 std::cout << x << std::endl; ss.clear(); ss.seekg(0, ss.beg); ss.get(x, sizeof(x)); // #3 std::cout << x; ss.clear(); ss.seekg(0, ss.beg); ss.getline(x, sizeof(x)); // #4 std::cout << x << std:endl; } ```