hello daniweb, I am new to C Programming,can anyone explain me the functionality and uses of getline(),with advantages and its disadvantages and where to use? thanks in advance

Your kidding right? You want us to read the docs to you?

can anyone explain me the functionality and uses of getline()

No, because there's no standard function called getline() and you didn't specify which library the getline() you're talking about comes from. But even if you did, I suspect the answer will be RTFM.

Well, the first thing you need to know is that getline() is not a standard C function (as of the C99 standard, at least to the best of my knowledge). It is an extension added in the GNU version of the stream I/O library, and while it is widely supported, it is not necessarily part of any given compiler's library, and is not that widely used.

(There is also a getline() function in the C++ <iostream> library, where it is standard, but it is a completely different thing than the C function. If you aren't sure which language the code you're reading is in, then chances are it's C++; post part of it and someone here should be able to tell you.)

The main advantage of getline() is that it is more secure than, say, gets() , in that it takes a size_t* argument that indicates the actual size of the buffer the line is being read into, and calls realloc() to re-size the buffer if the input exceeds the original buffer size (this assumes that the buffer was dynamically allocated in the first place, otherwise it would fail rather atrociously). This ensures that a buffer overrun cannot happen (short of running out of memory entirely), eliminating a common source of bugs and security flaws.

commented: RTOP! -4

pardon sir, I am reading C book by K & R , I ve seen using getline() in many places and i dont understand it's now where to use this getline().

pardon sir, I am reading C book by K & R , I ve seen using getline() in many places and i dont understand it's now where to use this getline().

So you've seen the book use it, and don't know where to use it? Well, a good start would be in the same situations that the book uses it. Alternatively, you could strive to understand the implementation provided in the book and then use it where you would otherwise write similar code manually.

I mean, it's really just a function that reads a (potentially partial) line of characters from standard input.

Just to make sure we're on the same page, here, is this the function you are referring to?

/* K&R's getline() function from p29 */
int getline(char s[], int lim)
{
  int c, i;

  for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    s[i] = c;
  if(c == '\n')
  {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';

  return i;
}

I think scanf and getline has the same function...
But, I dont know what would be its header file..
You cant use getline in C.

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.