私は昨夜からこの問題に悩まされていました.2番目の新鮮な目が助けになることを願っています.
問題は、無効な入力がuserIn
関数に入力された場合 (1 から 99 までの数値ではないもの)、while ループの最後のテスト printf がmain
"ERR = 1" を出力し、while がループしてuserIn
再度呼び出されることです。 . ここまでは順調ですが、有効な入力が入力されると、while ループの最後にテスト printf が出力され、"ERR = 0" が出力され、プログラムがハングします。「HELLO」というテスト printf が印刷されません。
理由についての提案は大歓迎です。
コード:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void userIn (int *x)
{
char b;
printf("Please enter a new value for Vm: ");
scanf(" %i",x);
while (((b=getchar())!='\n')&&(b!=EOF));
return;
}
int main (void)
{
int fd, x, err;
char *npipe = "/tmp/fms",
input[3];
struct stat info;
printf("\n");
//get user input
err = 1;
while (err)
{
userIn(&x);
if (x > 0 && x < 100) err = 0;
//else printf("\033[1A\033[K");
printf("ERR = %i\n",err);//TEST PRINTF
}
printf("HELLO");//TEST PRINTF
//if pipe exists
if ( (!lstat(npipe,&info)) && (S_ISFIFO(info.st_mode)) )
{
sprintf(input,"%i",x);
//write user input to named pipe created by 'parent.c'
fd = open(npipe, O_WRONLY);
write(fd, input, sizeof(input));
close(fd);
}
else printf("\033[0;31mNamed pipe doesn't exist, %i not passed.\n\n\033[0m",x);
return 0;
}