C function to read character - c

Is there a C function used to read characters from keyboard that does not return newlines ?
I have tried getchar() and scanf() .But both of it failed.
Please tell me if there is a function.

scanf() receives until a spacebar is entered ie.only one string is accepted . This can be overcome by using
scanf("%[^\n]s",str);.
It waits until newline is entered. gets() is also an alternative to receive string until newline is entered.

Related

How getchar() works when it is used as condition in while loop

I cant understand how the following code really works.
int main() {
char ch;
while((ch=getchar())!='\n')
{
printf("test\n");
}
return 0;
}
Lets say we give as an input "aaa". Then we get the word "test" as an output in 3 seperate lines.
Now my question is, for the first letter that we type, 'a', does the program goes inside the while loop and remembers that it has to print something when the '\n' character is entered? Does it store the characters somewhere and then traverses them and executes the body of the while loop? Im lost.
There are many layers between the user writing input into a terminal, and your program receiving that input.
Typically the terminal itself have a buffer, which is flushed and sent to the operating system when the user presses the Enter key (together with a newline from the Enter key itself).
The operating system will have some internal buffers where the input is stored until the application reads it.
Then in your program the getchar function itself reads from stdin which is usually also buffered, and the characters returned by getchar are taken one by one from that stdin buffer.
And as mentioned in a comment to your question, note that getchar returns an int, which is really important if you ever want to compare what it returns against EOF (which is an int constant).
And you really should compare against EOF, otherwise you won't detect if there's an error or the user presses the "end-of-file" key sequence (Ctrl-D on POSIX systems like Linux or macOS, or Ctrl-Z on Windows).
What you see is due to the I/O line buffering.
The getchar() functions doesn't receive any input until you press the enter. This add the \n completing the line.
Only at this point the OS will start to feed characters to the getchar(), that for each input different from \n prints the test message.
Apparently the printout is done together after you press the enter.
You can change this behavior by modifying the buffering mode with the function setvbuf(). Setting the mode as _IONBF you can force the stream as unbuffered, giving back each character as it is pressed on the keyboard (or at least on an *nix system, MS is not so compliant).

why do we have to press ctrl-d while reading data from scanf?

scanf("%lf",&b);
fgets(str, 100, stdin);
In the above code, fgets is not read until I add a space after %lf (eg. scanf("%lf ",&b);),
or press ctrl-d instead of enter in the terminal. Why is this so?
Don't mix fgets() and scanf().
scanf() leaves a newline (when you press ENTER key) which terminates the input reading of fgets() as fgets() would as soon as it sees a newline character, effectively your fgets() not reading anything at all.
fgets():
fgets() reads in at most one less than size characters from stream
and stores them into the buffer pointed to by s. Reading stops after
an EOF or a newline. If a newline is read, it is stored into the
buffer. A terminating null byte ('\0') is stored after the last
character in the buffer.
When you have a whitespace in the format string (scanf("%lf ",&b);), the scanf call would only return after you input a non-whitespace which will then be read by the fgets() call. This approach is error-prone. Because you will be forced to input something if you don't read anything further.
Please see: Why does everyone say not to use scanf? What should I use instead?
The scanf("%lf",...) will stop and wait for more input when it encounters whitespace in input. Adding a space to the format string causes the whitespace character (or characters) to be removed from stdin, and the function to return.
Another way to cause scanf() to return is to make it recognise an error of some form. One way is to make it seem like it has reached end of file. You haven't mentioned it, but your host system is some flavour of unix - typing CTRL-D (depending on terminal settings you may need to hit CTRL-D after a newline, or possibly enter it twice) makes it seem like end of file has been encountered. If you check the return value from scanf() it will return EOF in this case too. Different systems require different actions to trigger end of file though.
Either way, fgets(..., stdin) cannot be called until scanf() returns. Note that triggering end of file may (depending on terminal settings) also cause fgets(..., stdin) to return EOF (i.e. it won't necessarily read input).
It is a really bad idea to mix use of scanf() (or related functions) and fgets() on the same stream (stdin in this case), because they do interact in strange ways (each relies on behaviour in interacting with the stream that the other doesn't provide)

Spaces with scanf in a client to server message. C

I'm developing a client/server multithread program in C. I need to send a message from client to server with a scanf but when in the client puts a space the scanf see it as a newline. How can I read an input message with scanf including spaces? (and so the scanf don't see it as a newline?)
Thank you and I'm sorry for my English!
Use fgets() to get your input into a string and sscanf() to evaluate it. Since you just want what the user entered, you don't really need sscanf() in this case anyway
See this post

Read input from console in OSX

I'm using scanf() to read user input on terminal in a console application. scanf waits until the user hits the return key to read. Is there a way to read the user input on each keystroke?
The usual way would be to use the getch function from (the Mac port of) ncurses.
Note that while getchar reads a single character, it still normally does buffered reading, so you need to press 'return'/'enter' before it'll return.
getch() returns the character stream from stdin as it is typed.
char c = getchar();
It should do the trick.

can scanf be terminated on pressing some specific key other than enter

I have a situation here
i am taking input from user
using scanf can I terminate the scanf as soon as user presses the # key
please enlighten me on this
No, scanf() (or rather stdin, by default) is line-oriented so it needs to read a full line.
Look into ncurses or similar libraries for "raw" key-by-key input.

Resources