Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between read, readsome, get, and getline?

Tags:

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; } 
like image 542
user2030677 Avatar asked May 14 '14 20:05

user2030677


People also ask

What is difference between getline () and read () function?

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.

What is difference between Getline and Cin?

Definition. getline() is a standard library function in C++ and is used to read a string or a line from the input stream while cin is an object in C++ of the class istream that accepts input from the standard input device.

What is use of get and getline function in C++?

The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.


2 Answers

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.

like image 102
Rook Avatar answered Oct 26 '22 16:10

Rook


I've observed the difference between read() and readsome() on a flash filing system.

The underlying stream reads 8k blocks and the read method will go for the next block to satisfy the caller, whereas the readsome method is allowed to return less than the request in order to avoid spending time fetching the next block.

like image 34
quamrana Avatar answered Oct 26 '22 16:10

quamrana