Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-26-2005, 09:28 PM   #1
LQ Guru
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
gets library function in C

I've been messing around with some C code and when I use the "gets function", I get the following error,

: warning: the `gets' function is dangerous and should not be used.
An example from the book I am using is below. How would I rewrite that code so that it doesn't use "gets".

#include <stdio.h>
#include <string.h>
int main (void)
	char buffer [256];
	printf ("Enter your name and press <ENTER>:\n");
	gets (buffer);
	printf ("Your name has %d characters and spaces!\n", strlen (buffer));
	return 0;
Old 03-26-2005, 10:03 PM   #2
Registered: May 2004
Posts: 552

Rep: Reputation: 31
gets does not protect against buffer overflow, which is why its complaining.
Instead use
fgets will leave the end-of-line char on the of the buffer.
I always use this loop to remove it, this loop works on Windows and Linux.
The difference is that windows appends cr-lf and linux just lf.
char buffer[256];
char *p;
if (fgets(buffer,sizeof(buffer),stdin)) {
    while (p>buffer && *(p-1)<=' ') *(--p)='\0';
Old 03-26-2005, 10:36 PM   #3
LQ Guru
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Original Poster
Rep: Reputation: 77
Right, so I can use fgets. The syntax for "fgets" seems to be different to that of "gets" so I'll read up on it before trying your method and post some feedback later.
Old 03-27-2005, 02:04 AM   #4
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
Yes use fgets is better choice


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
library function was not kiiled. Jeon, Chang-Min Linux - General 0 07-30-2005 09:55 AM
Source Function Library dadepfan Linux - General 2 08-22-2004 11:06 PM
what is the function library of the basic graphics library in rethat9.0? zerwolve Red Hat 0 04-29-2004 09:18 PM
Library function to search for files on Linux vtluu Red Hat 3 04-16-2004 01:31 PM
C# using DotGNU, how do I use a library function? exodist Programming 3 02-22-2004 09:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration