Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function.
Here there is a little piece of code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

/* The last 3 libraries are included because in the real program I use some operations with sockets (socket, connect, getsockname, send, recv...) but I'm sure that the problem isn't here... */

int main()
{
char Ch = '0', Ch_Seq[64], inbuf[2];

  system\("clear"\);
  
  printf\("Insert a character \('q' = Exit\): "\);
  fflush\(0\);
  scanf\("%c", &Ch\);

  while \(Ch != 'q'\)
     \{
        printf\("Insert a string: "\);
        fflush\(0\);
        scanf\("%s", Ch_Seq\);
        /* There isn't length control, I know. Suppose to enter a valid string. */

        /* Asks for a new character */
        printf\("\\nInsert a character \('q' = Exit\): "\);
        fflush\(0\);
        scanf\("%c", &Ch\);
     \}
     
  printf\("End of program.\\n\\n"\);

}

This program (except for socket operations, obviously) works perfectly under MS-DOS, but when I try to run it under Linux (Mandrake 8.0) the second scanf functions in the while loop fails, because it reads the enter character of the previous scanf (which works correctly and stores the string in Ch_Seq).
For this reason the program can't never read the 'q' character, and enters in an infinite loop.

I have noticed that the only scanf that works correctly is the first, the one before the program enters in the while loop. Infact, if I run the program and I immediately enter a 'q' character, the program ends, writing "End of program.".

I have tried to use
scanf("%c\n", &Ch);
instead of
scanf("%c", &Ch);
but it doesn't work.

I have also tried with
Ch = getchar();
but the result is the same as described before.

I have temporary solved the problem replacing all the occurencies of
fflush(0);
scanf("%c", &Ch);
with
fflush(stdout);
read(0, inbuf, 2);
Ch = inbuf[0];

With this solution I have noticed a strange thing... If I write
read(stdin, inbuf, 2);
instead of
read(0, inbuf, 2);
it doesn't work! :confused:

Why?

However, I want to use anyway the scanf function and understand why under Linux it doesn't work.
Is there someone that could explain me in detail the behaviour of scanf function (and fflush) in Linux and how can I solve the problem?

I need to use scanf also because I want to port under Linux some programs written for MS-DOS that makes large use of this function.

I hope you can help me!

Thanks in advance, and excuse for my poor english! :slight_smile:

Change the "scanf %c" to "scanf %s" and see what happens....

Well, if I use scanf("%s", &Ch) the program works correctly, but the 'enter' character and the terminating character '\0' where are stored?
Isn't this an error? Don't these two characters overwrite the two bytes of memory next to the "Ch" variable?
If the method you shown me is ok, then when do we need to use "%c" or other functions that reads only a character (like getchar) ?
I still don't understand... :confused:

Another question: I want to read a string, scanning also spaces and tabs and check the length (and if necessary prompt the user to re-enter the string). Is the use of fgets followed by sscanf the best method I can use? Or are there any better methods?

Thanks a lot for your help! :slight_smile:

Yes, ideally you should increase the length of your input buffer since scanf will not consume the trailing newline. You may wish to read the "stdio" section of the C FAQ, in particular, question 12.20.

Thanks a lot for your help and for the "C FAQ" link, it's very useful!
:slight_smile: