Hey guys, I have been told that the getline function in C++ is a standard library member but is this true as the #include <stdlib> header file does not have to be included in order for getline to work?

Thanks.

Dani AI

Generated

Quick clarification for : in C++ the name "getline" actually refers to two different standard-library facilities — a member of the stream classes that fills a C-style buffer, and a non‑member template that fills a std::string. Both are standard, but they live in different places and behave differently, which is what caused the header confusion mentioned by and . (cppreference.com)

Header guidance (portable rules): the std::getline overload that accepts a std::string is provided with the string-related interfaces, so include <string>; the istream member overloads live with the stream classes (declare in <istream>, typically pulled in by <iostream>). Do not rely on one header implicitly bringing in another — include the headers your code actually uses for standards-conformance and portability. (cppreference.com)

Behavioral notes and a common pitfall: the istream member function writes into a fixed-size char buffer and will set failbit if the buffer limit is reached; std::getline that fills a std::string grows the string and discards the delimiter. A frequent trap is mixing formatted extraction (operator>>) with getline: the leftover newline causes an immediate empty read. Use an explicit skip (for example std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');) or the std::ws manipulator before calling std::getline. (en.cppreference.com)

Note about C: there is also a POSIX/C getline (ssize_t getline(char *, size_t , FILE *)) declared in <stdio.h>; it is a different API and not the same as C++ std::getline or istream::getline. Also avoid the old unsafe C gets() (deprecated/removed); prefer the safe C or C++ alternatives above. (gnu.org)

Recommended Answers

All 7 Replies

saying something is part of the standard c++ library doesn't mean it is declared in <stdlib> -- and there is no such header file. you probably mean <cstdlib>, which was derived form C language.

getline() is a method of std::string class and (I think) iostream.

#include <iostream>
#include <string>

int main()
{
  std::cout << "Enter someting" << std::endl;
  std::string line;
  std::getline(std::cin,line);
}

or

#include <iostream>

int main()
{
  char line[120];
  std::cout << "Enter someting" << std::endl;
  std::cin.getline(line,sizeof(line));
}

yes getline is apart of the standard library.
you still need to let the compiler know what libs you will be linking with.
I thought getline was in string.h.

yes getline is apart of the standard library.

I thought getline was in string.h.

NO! string.h is C, not C++. its declared as I posted above. C uses gets() and fgets(), not getline(). and both of them are declared in stdio.h, not string.h.

NO! string.h is C, not C++. its declared as I posted above. C uses gets() and fgets(), not getline(). and both of them are declared in stdio.h, not string.h.

I stand corrected... brain fart.... howerver, there does happen to be a stdlib.h.

/***
*stdlib.h - declarations/definitions for commonly used library functions
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This include file contains the function declarations for commonly
* used library functions which either don't fit somewhere else, or,
* cannot be declared in the normal place for other reasons.
* [ANSI]
*
* [Public]
*
****/

I stand corrected... brain fart.... howerver, there does happen to be a stdlib.h.

Yes there is -- but its C, not C++. and just look at the copyright dates that you posted, that file is at 8-21 years old! The most recent c++ standard was in (I think) 1998.

C++ system headers do not use .h file extensions. The c++ equivalent is <cstdlib>. If your c++ compiler does not support <cstdlib> then you need to toss that piece of trash into the bit bucket and get a more modern compiler, such as Dev-C++ or any of a number of others.

Your compiler may well compile stdlib.h with no problems -- but you run the danger that it will not contain up-to-date functions that are in <cstdlib>. I know VC++ 6.0/7.0/8.0 cstdlib just simply includes stdlib.h, but that by no means other compiler follow that.

>getline() is a method of std::string class and (I think) iostream.
It depends on which getline you're talking about, but taken as a whole, your statement is incorrect across the board. You're mixing up the getline member function provided by std::istream (which uses C-style strings) and the getline function provided for use with std::string in <string>. To summarize, using cin as an object of std::istream:

// getline member function
#include <iostream>
char s[SOME_SIZE];
std::cin.getline ( s, sizeof s );

// getline non-member function
#include <string>
std::string s;
getline ( std::cin, s );

>>your statement is incorrect across the board. You're mixing up the getline member function provided by std::istream (which uses C-style strings) and the getline function provided for use with std::string in <string>.

I didn't mix them up, nor was it incorrect -- I just mentioned them both in the same breath. I didn't say they were interchangeable, and I actually posted sample code that shows the difference, just like you did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.