Solved

gets()  not working..

Posted on 2003-12-01
10
192 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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