Solved

gets()  not working..

Posted on 2003-12-01
10
191 Views
Last Modified: 2010-04-22
hello!!

I want to input a string with spaces included.so i used gets() in place of scanf(). Its working correctly in one prog but in other prog, the program doesnt wait for the user input when i use gets(). scanf() is working fine. fflush(stdin) did not solve the purpose.What can be the problem?

Is there any alternative for gets() becoz during compilation also it gives a warning not to use gets()?

Paridhi
0
Comment
Question by:paridhi
10 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 9853653
Try fgets() instead. It takes three arguments:
char *fgets(char *s, int size, FILE *stream);

If your string is e.g. 80 characters long, and you are reading from stdin (which gets() does), use the following:

fgets(myString, 80, stdin);

0
 
LVL 24

Expert Comment

by:shivsa
ID: 9854578
u can use getc also.
man getc
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9854665
getc() only returns one character at a time. This can certainly be used, if you run it in a loop. This is not a direct replacement for scanf. BTW: Your compiler complains about using gets() because you can overrun your buffer with the input. If e.g. you create a 80 character buffer, and you type 81 characters, getc() has no mechanism of knowing how much space is available, it will copy 81 one characters into your 80 character buffer. This of course means that the 81st character is written into a memory area that does not belong to the buffer. This problem is resolved by using fgets() which does the same as gets() if you read from stdin, but you do specify the size of your buffer, so the function can verify that your input string does actually fit.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 125 total points
ID: 9855951
use fgets() as khkremer showed or use scanf()
scanf ( "%[^\n]s", string );
will read until it encounters a newline
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9890725
Did it work?
0
 

Author Comment

by:paridhi
ID: 9897371
Finally i used getc() only in a for loop..But initially the problem was the same with getc() also. Actually both the getc() and gets() were taking the previous input's Enter as an input character  and so the program was not waiting for the user's input .So i read the previous enter in another variable before calling getc or gets. SO the problem finally got solved.Thanks for all ur help..

Paridhi
0
 

Author Comment

by:paridhi
ID: 9897377
Finally i used getc() only in a for loop..But initially the problem was the same with getc() also. Actually both the getc() and gets() were taking the previous input's Enter as an input character  and so the program was not waiting for the user's input .So i read the previous enter in another variable before calling getc or gets. SO the problem finally got solved.Thanks for all ur help..

Paridhi
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9897636
Don't use gets(). Your compiler is right, it's not a safe function. If you type more characters than you buffer can handle, your application wil crash - or even more dangerous, somebody would use this buffer overflow to potentially crack your computer (depending on what your program does).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question