【发布时间】:2016-05-18 18:46:20
【问题描述】:
我有以下情况我有一个程序连续对文件进行一组操作,我希望在按下特定键时停止并执行另一组操作。
为此,我尝试使用带有
的字符的scanf
fcntl(0, F_SETFL, O_NONBLOCK);
和
while(feof(stdin))
但它没有按预期工作。
我搜索过,在某些地方有人说要使用select,但我找不到如何使用它。
谁能给我一些建议?
我的主要功能提供更多信息:
int main(int argc, char *argv[])
{
if(argc>1){
char* password;
char* password2;
printf("Password? ");
password = get_password();
printf("Repita a password? ");
password2 = get_password();
if(strcmp(password,password2)!=0){
printf("Passwords diferentes!\n");
exit(1);
}
FILE *fptr;
fptr = fopen("./regist", "r");
if(fptr == NULL) //if file does not exist, create it
{
fptr = fopen("./regist", "w");
}
fclose(fptr);
if(find_username(argv[1])){
printf("Utilizador ja existe\n");
exit(1);
}
add_user_regist(argv[1],password);
printf("Utilizador %s adicionado.\n",argv[1]);
exit(0);
}
char readbuf[250];
/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);
printf("Servidor iniciado.\nEm modo de espera de mensagens\n");
fcntl(0, F_SETFL, O_NONBLOCK);
while(1){
char c = getchar();
if(c=='q' || c=='Q')
exit(0);//by now only goes out
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 250, fp);
fclose(fp);
if(readbuf[0]=='U')
user_access(readbuf);
else if(readbuf[0]=='W')
who_online(readbuf);
else if(readbuf[0]=='R'){
char* tmp;
strtok(readbuf,":");
tmp = strtok(NULL,";");
remove_online(tmp);
printf("# %s fez logout\n",tmp);
}
else if(readbuf[0]=='F'){
process_msg(readbuf);
}
}
return(0);
}
【问题讨论】:
-
在什么操作系统上?这改变了答案
-
发布您的代码。为什么使用
fcntl不起作用? -
linux ,我使用的 fcntl 但是当我按下“Q”时,我的键是什么,scanf 可能不会继续进行第一次操作
-
如果你在 linux 上使用类似 SIGINT 的信号可能会很有趣。当您按 ctrl-c 时,您将收到此信号。
-
您希望
while(feof(stdin))做什么?如果您需要getch函数,请参阅my answer here。
标签: c linux ubuntu-14.04 stdin nonblocking