次のプログラムを使用して、ファイル記述子 '0' (STDIN) からユーザー入力を読み取ろうとしています。以前は問題ありませんでしたが、プログラムの他の部分をいくつか変更した後、入力の読み取り中にセグメンテーション エラーが発生しました。「FD_CLR(0, &readfds)」も削除して、機能するかどうかを確認しましたが、機能しません。どこに問題があるか確認していただけますか?
char *userInput;
FD_ZERO(&masterfds);
FD_SET(0, &masterfds);
FD_SET(udp_con, &masterfds);
maxfds = udp_con;
while(exit == false)
{
readfds = masterfds;
selectFunc = select(maxfds+1, &readfds, NULL, NULL, &tv);
if(selectFunc < 0)
{
message("error in select");
exit = true;
}
else if(selectFunc == 0) //If there is a timeout
{
}
else //If a file descriptor is activated
{
if(FD_ISSET(udp_con, &readfds)) //If there is an activity on udp_con
{
/*read the udp_con via recvfrom function */
}
if(FD_ISSET(0, &readfds)) //If There is an input from keyboard
{
/* When it reaches to this part, the program shows a "segmentation fault" error */
fgets(userInput, sizeof(userInput), stdin);
int len = strlen(userInput) - 1;
if (userInput[len] == '\n')
{
userInput[len] = '\0';
}
string str = userInput;
cout<<"The user said: "<<str<<endl;
commandDetector(str);
FD_CLR(0, &readfds);
}
}
}