What's the difference between read, readsome, get, and getline?
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.
Difference between istream::get(char&) and operator (char&)
The difference depends on when there is a whitespace character on the stream buffer.
Consider the input ' foo'
char c;
cin.get(c);
Will store ' '
in c
However
char c;
cin >> c;
Will skip the whitespace and store 'f'
in c
Why are there two different getline() functions (if indeed there are)?
Bear in mind that the Standard library is composed from 3 (main) parts: IOStream, String and STL, plus some tossed in goodies and the C-headers.
I don't see anything weird in having those parts loosely coupled (though I wish it was not the case).
Other incongruities include: std::string::length
vs std::string::size
, the latter having been added for interface compatibility with the STL and the former having been retained for compatibility with older code.
getline in istream and getline in basic_string
text.c_str()
returns a const char *
. You may not use it to modify the contents of the string, in any way. It only exists so that you can pass the string data to old C API functions without having to make a copy. You are not allowed to make changes because there is no way that the string object that holds the data could possibly find out about them, and therefore this would allow you to break the string's invariants.
Furthermore, std::getline
accepts completely different parameters. (You would know this if you took two seconds to type 'std::getline' into Google.) The error means exactly what it says: "no matching function for call" means "you can't call the function with these kinds of parameters", because every overload of the function accepts something different (and incompatible).
std::getline
accepts these parameters:
- A stream. You have to pass this because otherwise it doesn't know where to read from.
- A string object to read into. NOT a char buffer.
- Optionally, a line delimiter char (same as the stream getline member function).
There is not really any such function as "cin.getline". What you are calling is the member function "getline" of the object "cin" - a global variable that gets defined for you when you #include <iostream>
. We normally refer to this according to what class the function is defined in - thus, std::istream::getline
.
std::istream::getline
accepts these parameters:
- A char buffer.
- Optionally, a line delimiter char.
It does not need a stream parameter because it is a member function of the stream: it uses whatever stream we called it with.
std::cin.getline( ) vs. std::cin
In case with char*, std::cin.getline
getting line, instead of std::cin
getting first word.
Converting istream operator to istream getline
This line
friend istream & getline( istream & input, Result & Re, ',' );
won't compile because you have a constant as the third parameter instead of a parameter declaration.
In your case, because you hard code ','
in the call to getline inside the method, you don't need to pass the comma in at all.
friend istream & getline( istream & input, Result & Re);
Similarly, when you define the function, you don't need the ','
in the function definition:
istream& getline( istream & input, Result & Re)
{
getline(Re.Coursename, input, ',');
return input;
}
Note that you still need the comma in the internal call to getline
.
This should fix up the compiler errors you have listed, you will still need to debug your code if it doesn't work.
Related Topics
Nested Class' Access to Enclosing Class' Private Data Members
What Are the Advantages and Disadvantages of Implementing Classes in Header Files
Create Shared_Ptr to Stack Object
Parse String Containing Numbers into Integer Array
Constructor Arguments from Tuple
Double Dispatch/Multimethods in C++
Concatenate Char Arrays in C++
Will Consteval Allow Using Static_Assert on Function Arguments
Wrap Overloaded Function via Std::Function
Are Non Dereferenced Iterators Past the "One Past-The-End" Iterator of an Array Undefined Behavior
Why Does Stack<Const String> Not Compile in G++
How to Get the Address of the Std::Vector Buffer Start Most Elegantly
Why Is the New Operator Allowed to Return *Void to Every Pointer-Type
C++. Error: Void Is Not a Pointer-To-Object Type
Template Base Constructor Call in Member Initialization List Error
How to Get at the Exception Information When Using Minidumpwritedump Out-Of-Process