answersLogoWhite

0

Difference between fgets and gets

Updated: 8/9/2023
User Avatar

Wiki User

12y ago

Best Answer

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.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Both fgets() and gets() read a line terminated with a newline terminator, while gets() reads from stdin and fgets() reads from a specified file. Also, gets() strips the trailing newline from the result.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between fgets and gets
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between gets and puts?

Direction: gets: from standard input to memory puts: from memory to standard input note: 'gets' is unsafe, use 'fgets' instead


What is the use of gets function in c language?

Nothing, it is a security hole. Use 'fgets' instead.


How do you input a string in c?

just use fgets() from file stdio.h (not gets: it is security hole)


What is difference between scanf and cin?

scanf is a function (available in C and C++)cin is an istream object (C++ only)Advice: when in doubt, use fgets+sscanf


In gets function which will you use to fix the size of the string and specify the number of characters?

Never use gets, use fgets instead. And strlenafterwards.


Which function used to read characters into an array specified by its parameter?

Examples: recv, read, fread, gets, fgets, scanf, fscanf


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.


Is that any other way to input data in c language other than scanf function?

read, fread, gets, fgets, getc, fgetc, getchar, getch


How do you read line by line of a file in c program?

read, fread, fgetc, fgets, fscanf etc... use the help/manual


How many arguments that fgets function requires?

Three.


What is the difference between growth and development?

in growth the organism gets larger, in development the organism gets more complex


What is one similarity and difference between the Sahel and savanna?

A similarity between a Sahel and the savannah is it gets rainfall :)