这就是我正在尝试做的事情:
我要求用户输入一些句子
每当他想停止时,他就会按 Q(大写字母)
- 然后应该开始下一个处理。
我现在得到的:
- 系统会提示用户输入任意数量的句子
- 但是当他按 Q 时,控件不会从 while 循环转移到下一条指令。
这是一个 FIFO 程序。如果您发现任何其他错误,请报告它们。谢谢!
代码如下:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
void main()
{
int i=0,fd;
char str[500]="",ch;
char *myfifo="/home/rahulp/Desktop/myfifo";
mkfifo(myfifo,0666);
printf("Enter the sentences:");
while((ch=fgetc(stdin))!='Q')
{
printf("ch===%c",ch);
str[i++]=ch;
}
str[i]='\0';
fd=open(myfifo,O_WRONLY);
if(fd<0)
{
printf("Cannot open fifo");
}
else
{
write(fd,str,strlen(str));
}
close(fd);
unlink(myfifo);
}
最佳答案
忽略 mkfifo 发生问题的可能性,这就是您应该如何读取 stdin:
int ch;
printf("Enter the sentences:");
fflush(stdout);
while ((ch = fgetc(stdin)) != EOF)
{
if (ch == 'Q')
break;
printf("ch=%c\n", ch);
str[i++]=ch;
}
str[i]='\0';
关于c - While 循环不能与 fgetc 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36459462/