LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 73
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,

Code:
: 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".

Code:
#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, 11:03 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
gets does not protect against buffer overflow, which is why its complaining.
Instead use
fgets(buffer,sizeof(buffer),stdin);
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.
Code:
char buffer[256];
char *p;
if (fgets(buffer,sizeof(buffer),stdin)) {
    p=buffer+strlen(buffer);
    while (p>buffer && *(p-1)<=' ') *(--p)='\0';
}
 
Old 03-26-2005, 11:36 PM   #3
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Original Poster
Rep: Reputation: 73
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, 03:04 AM   #4
nixcraft
Member
 
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
 
  


Reply


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 10:55 AM
Source Function Library dadepfan Linux - General 2 08-23-2004 12:06 AM
what is the function library of the basic graphics library in rethat9.0? zerwolve Red Hat 0 04-29-2004 10:18 PM
Library function to search for files on Linux vtluu Red Hat 3 04-16-2004 02:31 PM
C# using DotGNU, how do I use a library function? exodist Programming 3 02-22-2004 10:41 PM


All times are GMT -5. The time now is 01:08 PM.

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