Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getline in istream and getline in basic_string

Tags:

c++

string text;
getline(text.c_str(),256);

1) I am getting an error "error: no matching function for call to 'getline(const char*, int)" What's wrong in the above since text.c_str() also returns a pointer to array of characters.

If I write like this

char text[256]
cin.getline(text, 256 ,'\n'); 

it works fine. What's the difference between cin.getline and getline?

2) How come

text string;
getline(cin,text,'\n') 

accepts the whole line as the input. Where is the pointer to array of characters in this one?

like image 568
Cipher Avatar asked Dec 30 '10 17:12

Cipher


People also ask

What is the difference between Getline and CIN Getline?

The main difference between getline and cin is that getline is a standard library function in the string header file while cin is an instance of istream class. In breif, getline is a function while cin is an object. Usually, the common practice is to use cin instead of getline.

How do you use Getline with multiple delimiters?

std::getline() does not have an option for multiple alternate delimiters. The correct way to parse this kind of input is to use the default std::getline () to read the entire line into a std::string , then construct a std::istringstream , and then parse it further, into comma-separate values.

Can you use Getline on Stringstream?

You cannot std::getline() a std::stringstream ; only a std::string . Read as a string, then use a stringstream to parse it. Hope this helps.

Can I use Cin and Getline together?

The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage.


2 Answers

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.

like image 122
Karl Knechtel Avatar answered Oct 14 '22 08:10

Karl Knechtel


I don't really get what the questioner is trying to do.

C++ allows function overloading, so the compiler is looking for a free function called getline that matches the parameters you have passed, and no such function exists, nor should it exist (what would getline(const char*, int) do anyway?)

The question has been asked many times why getline(istream&, string&) is a "free" function and not part of the iostream interface. Answers suggested have been that iostream outdates STL or that iostream has no dependent on the basic_string class (anywhere, which is also why opening files is done with raw pointers), and Herb Sutter would commend making getline a free-function because he feels class member functions should be minimal (and std::string has far too many, eg the find functions which could be free ones that use the class).

One thing about that function though is how useful it is as you do not need to pre-allocate a buffer and "guess" how big to make it. (Having said that if you read from a big file into a std::string you could bad_alloc if there are no newlines to be found!).

like image 36
CashCow Avatar answered Oct 14 '22 10:10

CashCow