The user types a string, possibly separated by tabs, spaces and "enters" (CRs). I need to receive all of it; the problem is that gets() function stops the scan when the user presses the "Enter" key. Is there another way to do it? I cannot use any other function except for scanf and gets.
+1
A:
First of all: gets()
is really evil. DONT EVER USE IT.
It is not possible to write a correct program using gets()
.
Consider read()
, getchar()
, ...
Johannes Weiß
2009-12-15 22:10:36
Calling things evil is unhelpful. DONT EVER DO IT. It would be more helpful if you explained the shortcomings of `gets()` (buffer overflows, stripping of newlines) so that the OP could take knowledge to her superiors to push for an alternative.
P Daddy
2009-12-15 22:16:44
Calling gets() evil is accurate. The normal recommendation is fgets(), of course. An explanation of why would improve things, though.
Jonathan Leffler
2009-12-15 22:37:51
The infamous Morris worm, exploited the gets function within the unix's finger program and overwrote the stack and that is how it spread like wildfire, the rest they say is history.
tommieb75
2009-12-15 22:56:50
That gets is evil is well known and need not be explain yet again. A quick google search will easily answer this question.
Alex
2009-12-16 02:16:47
Also, n1336 (http://www.open-std.org/jtc1/sc22/wg14/www/projects#9899) deprecates `gets`, so it might *finally* be removed from the C standard. Of course, support for C1X might take 10+ years, so who knows when it will be actually removed from implementations :-),
Alok
2009-12-16 02:55:33
+1
A:
Use a loop. Keep getc
ing (putting the results into some large-enough buffer), until you encounter EOF
.
Anon.
2009-12-15 22:10:59
If you *have* to use `gets`, you're going to need to repeatedly call it until you've got all the data. How does your professor expect you to do that without a loop?
Anon.
2009-12-15 23:13:35
Probably recursion, but I'm trying my hardest to avoid that. If there's no other solution, I guess I'm gonna have to use recursion.
Lady M
2009-12-15 23:19:12
You could write your iterative version, than convert it to tail-call recursive easily enough. Keeps to the letter of "no loops" while completely violating the spirit of it.
Anon.
2009-12-15 23:47:07
A:
Why do you need the "Enter"? You know by the fact that gets returned that a carriage return (or end of file) was detected.
Trent
2009-12-15 22:12:30