Hello All
I want to accept the string from user till he press "enter" key.
For that I used gets() function on Windows.
But the same program not run on Linux.
Can any one tell me alternative for this function?

gets() is broken by design and you should use fgets() instead, but unless "program not run" means you're scared of a compiler warning to that effect, fgets() will likely suffer the same issue because the problem is in your code or your assumptions about the environment. You should learn to blame yourself first, because that's where most problems lie.

Now, please post you code so we can do more than guess at what you screwed up.


gets() is broken by design and you should use fgets() instead, but unless "program not run" means you're scared of a compiler warning to that effect, fgets() will likely suffer the same issue because the problem is in your code or your assumptions about the environment. You should learn to blame yourself first, because that's where most problems lie.

Wait a second. fgets() is for reading from files. It has nothing to do with what yashsaxena wants.

I think what's going on yashsaxena's side is the problem with gets() function itself. It's not something new & it's about the compilers rules that they are based on.

e.g. If you've continuous gets() functions or a gets() function after a scanf() function then compilers skip that gets() function.

Sure, I can guide you better if you've shared your code but my best assumption is that your problem is the one above. If you put just a dummy gets() function before, it should be okay.

>>Wait a second. fgets() is for reading from files

Narue is correct -- why do you believe that? The keyboard is treated just as any file system, just use stdin as the file handle.

>>e.g. If you've continuous gets() functions or a gets() function after a scanf() function then compilers skip that gets() function
That problem is not compiler related. The compiler skips nothing, it doesn't toss out code for no good reason.

In the problem you described, the solution is to clear the input buffer after scanf() call, such as removing the '\n' (Enter key) from the keyboard buffer. In any event gets() should never ever under no circumstances in any lifetime be used. Use either fgets() or gets_s() (if your compiler supports that).

Didn't know that. Sorry guys. :))

Also why don't they just remove gets() function from compilers if it's so problematic?

Because its in the ISO standard, so compiler writers have little choice. Also, if they removed the function then it would break all legacy code that used it.

Also why don't they just remove gets() function from compilers if it's so problematic?

Backward compatibility. Removing features has a tendency to break a lot of programs that use the feature, and gets() is unfortunately very common. The good news is that gets() has finally been voted off the island and will likely be removed in the next C standard. The bad news is that compilers will likely support it for the foreseeable future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.