When does fgets start reading? - c

If I were to print a prompt to stdout:
printf("> ");
and then I said
fgets(tester, 80, stdin);
would fgets read the whole line including the "> " or would it start after that?

The prompt is output (on stdout). fgets is reading input from stdin. So no, it won't read your prompt.
Your terminal displays stdout, stderr and stdin all together, but that doesn't mean there isn't an underlying distinction between them.
stdout is typically line buffered, and printf("> "); lacks a new line, so the output may not appear on a terminal display until later. fflush(stdout) to insure it is outputted before the fgets().
printf("> ");
fflush(stdout);
fgets(tester, 80, stdin);

The fgets won't read anything because the printf will write on STDOUT and not on STDIN.

No. printf will reflect the stdout. It will never effects your stdin. The str buffer will reflect all the characters you give as an input after >.
Test for the same can be done like this:
#include<stdio.h>
int main() {
char str[80];
printf(">");
if( fgets (str, 60, stdin)!=NULL )
{
puts(str);
}
return 0;
}

Related

EOF key not outputting NULL in C STDIN

I use CLION as my IDE.
I have the following code snippet which is suppose to reverse lines of user input in STDIN.
void reverse(){
char str1[100];
fgets(str1, 100, stdin);
printf("%s", str1);
}
Now when I hit Ctrl+D on my machine it prints out ^Z in STDOUT.
I am trying to make, that fgets() reads each line until the user hits EOF key, how would I do this in C?
I hit Ctrl+D this registers as ^D to stdin
and the printf statement prints out ^Z.

Getting stdin without dropping a line in the terminal

I have a multi-threaded application and am trying to format my output in the terminal with escape codes. The only issue I am having is that when gathering input, I cannot place an output mutex around fgets since it will block output until something is input. Consequently, there is this small window where other threads gain access to the output mutex and print before I can erase the newline created by fgets with \b.
Is there a way around this? Or is there a function in C that accepts input without entering a newline after pressing ENTER?
My code for input is as follows:
pthread_mutex_lock(&mutex_stdout);
fflush(stdout);
printf("\33[2K\r");
printf("Accept input: ");
fflush(stdout);
pthread_mutex_unlock(&mutex_stdout);
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
pthread_mutex_lock(&mutex_stdout);
buffer[strcspn(buffer, "\r\n")] = 0;
printf("\b");
pthread_mutex_unlock(&mutex_stdout);

Write to stdout and read from stdin in C

I am writing two c files that one is to write(by stdout) and another is read(by stdin).
But the read code is always hangs with read(), I have tried fread function but useless.
Can someone give advice?
Write example:
int main() {
char *a_string="Hello";
write(fileno(stdout), a_string, strlen(a_string)+1);
return 0;
}
Read example:
int main() {
char buffer[100];
read(fileno(stdin), buffer, 100-1);
printf("buffer=%s\n", buffer);
return 0;
}
The read code is always hangs with read(), I have tried fread function but useless.
This is because read and fread, unlike fgets and similar functions, do not quit reading when the user inputs a newline (presses Enter). It waits until EOF is read or until the specified bytes have been read.
To send EOF to read you must press Ctrl+D.
You need to input the EOF to stop, in Linux,it's Ctrl+D.

c fgets not waiting for input

I have 2 fgets into my code and both of them aren't wait for input...
This is an example of the first fgets...
printf("Insert path: ");
if(fgets(dirpath, BUFFGETS, stdin) == NULL){
perror("fgets dir path");
close(sockd);
}
and, as i've written before, also the next fgets is not waiting for my input :(
Before the first fgets i have 2 scanf("%ms", &string); (if this could be the trouble).
i think scanf does not read in the linebreak. You can try to read it in first with an additional fgets after scanf().

How to flush the console buffer?

i have some code that run repetedly :
printf("do you want to continue? Y/N: \n");
keepplaying = getchar();
in the next my code is running it doesnt wait for input.
i found out that getchar in the seconed time use '\n' as the charcter.
im gussing this is due to some buffer the sdio has, so it save the last input which was "Y\n" or "N\n".
my Q is, how do i flush the buffer before using the getchar, which will make getchar wait for my answer?
Flushing an input stream causes undefined behaviour.
int fflush(FILE *ostream);
ostream points to an output stream or
an update stream in which the most
recent operation was not input, the
fflush function causes any unwritten
data for that stream to be delivered
to the host environment to be written
to the file; otherwise, the behavior
is undefined.
To properly flush the input stream do something like the following:
int main(void)
{
int ch;
char buf[BUFSIZ];
puts("Flushing input");
while ((ch = getchar()) != '\n' && ch != EOF);
printf ("Enter some text: ");
if (fgets(buf, sizeof(buf), stdin))
{
printf ("You entered: %s", buf);
}
return 0;
}
See Why fflush(stdin) is wrong and Flush the input buffer.
use fflush() and flushall() before printf
As far as I know, flushall is not POSIX. In order to flush a console buffer in a standard way, you can simply use the command:
fflush(NULL);
This topic seems to be a bit old but I hope this can still help the others.

Resources