Quantcast
answersLogoWhite
notificationBell
Computer Programming
C Programming
The Difference Between

Between fgets and gets which function is safe to use and why?


Top Answer
User Avatar
Wiki User
Answered 2008-02-22 11:07:04

gets is an insecure function, its careless use can lead to errors. If you

want to use gets, consider using fgets instead, supplying stdin as

the file reference parameter.

The gets function waits until a line of input is available (unless one is already

available), and consumes the whole line including the ENTER/newline at the end.

The characters on the line are stored in the string parameter, except for the

ENTER/newline, which is discarded.

returns NULL on end-of-file, otherwise the parameter s.

The parameter given to gets must be an already allocated array of characters, not an

uninitialised char * pointer; gets will never allocate memory.

{ char a[100]; gets(line); // This is correct

{ char a[100]; char *s; s=a; gets(s); // This is correct

{ char *s; s=new char[100]; gets(s); // This is correct

{ char *s; gets(s); // This is WRONG

The array given to gets must be big enough to hold any line that could conceivably be

input. C++ and C are incapable of telling how long an array is. If it is not long enough

for the data that is read, other data (and perhaps program code) will be overwritten.

Thus gets is not a safe function for use in critical applications.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Still Have Questions?

Related Questions

Is puts safe to use?

There is nothing wrong with 'puts' but you should avoid the using of 'gets', and even 'fgets' has a disadvantage: if the input contains a binary zero, you will lose data.


What is gets() function in c programming language?

The gets() functions reads input from stdin and writes it to a user-defined buffer. The function has the following declaration in <stdio.h>: char* gets (char* str); // WARNING! Deprecated since 2011 The function is unsafe because the function won't stop writing to the buffer until a newline or end-of-file is encountered in stdin. If the buffer is too small, the function will overwrite memory beyond the bounds of the buffer. If available, use the gets_s() function instead. This allows you to specify the length of the buffer and thus prevent buffer overruns: char* gets_s (char* s, rsize_t n); Alternatively use the fgets() function passing stdin as the input stream: char* fgets (char* s, int n, FILE* stream); POSIX 2008 also provides the getline() function as a safe alternative to gets(). However, this function resizes the buffer dynamically and must be manually released (free'd) when no longer required.


Difference between safe mode and safe with commandprompt?

Both "safe mode" and "safe mode with command prompt" runs with essential drivers that is necessary for the computer to function. Difference is, Safe mode runs with "Explorer.exe" shell and "safe mode with Cmd prompt" runs with "cmd.exe".


How safe is a fixed annuity with pacific life?

That's about as safe as it gets.


How the man feel safe?

he feels really safe when he gets a massive BONER



Why is the Volvo C70 safe?

They are very safe cars the metal they use for the cars is strong and when you crash it gets pushed in but its safe ALL volvos are safe


What is the function of the aqueous humour?

The Aqueous Humor is the fluid that is in between the Cornea and the Lens. It help keeps the front of the eye safe from dust, wind, etc.


What is function of cell wall?

To keep your cells safe.


What do snails do when it gets cold?

they snug in their shell. safe and sound......!


How safe is it for my boyfriend to give me oral if he doesn't have a cold sore present but hardly ever gets them?

If he doesn't have any signs or symptoms of a cold sore you're safe to have oral sex. Oral herpes is not likely to be passed in between break outs.


What is the function of a teddy bear?

the function of a teddy bear is to warm peoples hearts and make them feel safe during the night


Which function key is used to access Safe Mode in Windows 9X?

It Should be F5, CTL+F5 for Safe mode with no compression and F6 for safe mode in networking.



What Is the combination for the safe on Club Penguin?

There is no permanent code for the safe, it gets changed at every moment so knowone cheats on the mission.


What is the basic function of a hotel?

The basic function of a hotel is to house people who are traveling. Hotels serve as a safe place to spend the night.


Is it safe to take gabapentin and vicodin?

hellll ya it gets you highhhh as balls


Is it safe to smoke sage?

yes it gets u higher than weed


Is virtuagirlhd safe?

No the program gets crap in your computer and it will be difficult to uninstall it, I advise you not to get it.


Is it safe to blow marijuana smoke in your ear?

Yes, and it gets you really stoned.


Is it safe taking lamotrigine if you are pregnant?

Yes it is safe. Although the baby could come out extremely attractive and will become a homosexual when it gets to 30.


What is a canister using in laboratory and its function?

A canister is used in a laboratory setting. The function of a canister is for the safe storage and transportation of flammable and volatile items.


How do rabbit survive in the cold winter?

They gorww long fur.To keep them safe from predetors there fur gets camafloged depending on the season. They gorww long fur.To keep them safe from predetors there fur gets camafloged depending on the season.



How do bananas from the forest get to the factory?

People take the bannanas from trees and ship them to the factory, gets inspected if a safe one. Gets shipped off to store


Still have questions?

Trending Questions
How to Make Money Online? Asked By Wiki User
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Unanswered Questions