- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个程序在开始 for (;;)
循环之前运行几个步骤。
我要能破;此 for
循环使用击键(例如 'e
' 退出)
我试过 scanf_s
但我认为我没有正确使用它们。如果我尝试写入 scanf_s
,循环就不会启动。
我对 C 编程不是很熟悉,所以我有点吃力。
有人可以阐明这一点吗?
额外信息:
操作系统为Win 8.1。
程序是一个TCP服务器程序。它创建一个套接字并开始监听。然后它开始在 for
循环中接受客户端连接。然后开始另一个 for
循环以接收来自客户端的消息。
编辑:使用 this我已经设法用 s
停止了第二个 for 循环。添加了对简化代码的更改(如下所示)。
添加:
if (_kbhit()) {
key = _getch();
if (key == 's');
break;
}
这里是一些简化的代码:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// prelim info goes here
// like SOCKET
// and struct sockaddr_in
// Initialize Winsock
WSAStartup();
// Create socket
mysocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// server parameters go here
// like sin_addr and sin_port
bind(mysocket);
// start listening
listen(mysocket, SOMAXCONN);
// begin the damn for loops
for (;;)
{
myclient = accept();
//second for loop
for (;;)
{
receivedata = recv(myclient);
printf(<data>)
if (_kbhit()) {
key = _getch();
if (key == 's');
break;
}
}
}
// close socket
closesocket(mysocket);
// call WSACleanup
WSACleanup();
return 0;
}
感谢所有提供链接和有用答案的人。事实证明,外循环的问题比破坏它要复杂得多。然而,如果任何 future 的访问者想知道如何通过键盘按下来打破循环,这已经实现了。请参阅正确答案或我的简化代码。
最佳答案
您可以尝试使用 kbhit
和 getchar
来检查击键并相应地处理操作。查看此帖子:Return pressed key without enter for confirmation
当按下 e
时,这段代码会中断 while 循环:[代码更新了一个额外的 while 循环]
#include <stdio.h>
#include <conio.h>
int main(){
int ch = '1';
while(ch != 'e'){ //outer while for accepting connection
printf("waiting for connection\n"); //do something
while(ch != 'e'){ //inner while for checking key press
if(kbhit()) //if key is pressed
ch = getch(); //get the char and set ch to 'e'
}
}
return 0;
}
使用 break 的旧代码:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main(){
int ch;
while(1){
printf("*");
if(kbhit()){ //if key is pressed
ch = getch(); //get the char
if(ch == 'e') //if pressed key is 'e'
break; //break while loop
}
}
return 0;
}
关于c - 在 C 中用键盘打破 FOR 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25946367/
有人可以向我解释该声明在C++中的含义吗?我从未见过这样的声明,只是对它的含义和作用感到困惑: int ix((dx > 0) - (dx > 1)); 最佳答案 您可以在括号或花括号中使用初始化程序
我有一个带有单词的mysql数据库。我用 while 语句打印所有单词。所以我觉得: 马铃薯番茄生菜 一切正常,但我想按长度对单词进行排序。我试过: if(strlen($go['words']) =
我忠实的路径遍历方法不再有效——它将空格视为分隔符。好久没做批处理编程了。 使用 FOR 循环时,唯一允许使用分隔符的是 FOR/F 选项。 我不想创建一个包含路径的临时文件,希望做如下的事情: C:
新建一个表: ? 1
我有一些带有多行块的文本文件,例如 2011/01/01 13:13:13,, Some Certain Text,=, [ certain text [
我想在 Vim 中文件的不同部分之间进行一些很好的分离: 我想用#'s 填充一行,然后在中间写上我的标题: ############################# 居中标题############
我该如何逃生 "*"至 "\*"在clojure?似乎无法让它工作: (s/replace "A*B" #"*" "*")生产 "A*B" (当然) (s/replace "A*B" #"*" "\*
这周我一直在努力更熟悉 C。我一直在阅读C Primer Plus (5th Edition) 但是我仍然在使用变量和指针时遇到了一些麻烦。 这是我用来测试的脚本: int main (int arg
在 Dart 中,初始化 List 有什么区别?使用 new 运算符并使用文字对其进行初始化? 情况1: List args = new List(2); args[0] = 1; args[1] =
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我有一个名为 gen 的数据框,如下所示 A B C D E 1 NA 4.35 35.3 3.36 4.8
我有一个字符向量,如下所示: "Internet" "Internet" "-1" "-5" "Internet" "Internet" 我想替换所有负数值的值(-1、-5 等
我想知道为什么 CMake 中的变量经常用美元符号和大括号括起来。例如,我看到这个电话in a CMake tutorial . include_directories(${PROJECT_BINAR
我正在尝试做这样的事情 $this->db->count_all("grant_money")->where('id',5); 这可能吗? 如果有任何其他方法可以做到这一点,请告诉我。谢谢 我想像上面
为什么这是有效的: int a = 5; int *aPtr = &a; printf("%i", *aPtr); 但这不是: int a = 5; int aPtr = &a; printf("%i
假设我有一个格式为“11.23.13”的日期字符串,我想用“/”替换每个点,使其看起来像“11/23/13”。 这是我的代码,但它无法正常工作,因为正则表达式看到“.”并将其解释为匹配每个字符而不是新
如何在键盘输入的字符处打印*? 例子: 如果我在控制台中输入:mouli,那么它应该将 m 替换为 *,然后是 o用 * 等等。 最佳答案 使用标准 API 无法解决此问题。如果这确实是一个明确的要求
我最近开始学习 Javascript,同时对卡在这段代码中的代码进行了一些实验: var k = { ab: "hi", func: function() { cons
我需要用“.”替换第一列中的重复项 例如: name1 name1 name1 name2 name2 name3 name3 我需要输出: name1 . . name2 . name3 . 我有这
我有以下两个表 education 和 jobs,每个表都有时间戳字段。在续集语句中,我想选择并确定两个表中保存的两个时间戳中哪个是最新的。 我已经尝试了以下但并不愉快; SELECT e.Sta
我是一名优秀的程序员,十分优秀!