I was trying to read input from the terminal using read() in a while loop as a condition, but the code doesn't get executed until the loop ends.
Here is my code:
#include<unistd.h>
int main(void)
{
char ch;
while(read(STDIN_FILENO,&ch,1) == 1 && ch != 'q')
{
printf("success");
}
return 0;
}
it gives me the output :
t
fgr
vr
q
successsuccesssuccesssuccesssuccesssuccesssuccesssuccesssuccesssuccess
Even when I try to normally read data, the unread data in the buffer ends up like this.
code:
#include<unistd.h>
int main(void)
{
char ch;
read(STDIN_FILENO,&ch,1);
return 0;
}
output in terminal
user1@lap:~/Desktop$ ./program2e3
ted
pradeep@LLP:~/Desktop$ ed
Why does the unread data become the next command?