相关疑难解决方法(0)

C非阻塞键盘输入

我正在尝试用C语言编写一个程序(在Linux上)循环,直到用户按下一个键,但不应该要求按键继续每个循环.

有一个简单的方法吗?我想我可以做到这一点,select()但这似乎很多工作.

或者,有没有办法在程序关闭之前捕获ctrl- ckeypress进行清理而不是非阻塞io?

c linux asynchronous input nonblocking

76
推荐指数
5
解决办法
10万
查看次数

如何避免使用getchar()按Enter键

在下一个代码中:

#include <stdio.h>

int main(void) {   
  int c;   
  while ((c=getchar())!= EOF)      
    putchar(c); 
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我必须按Enter下来打印我输入的所有字母getchar,但我不想这样做,我想要做的就是按下这封信,然后立即看到我重复介绍的信,而不是按下Enter.例如,如果我按下'a'字母,我想在旁边看到另一个'a',依此类推:

aabbccddeeff.....
Run Code Online (Sandbox Code Playgroud)

但当我按'a'时没有任何反应,我可以写其他字母,只有当我按下时才会出现副本Enter:

abcdef
abcdef
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我正在使用cc -o example example.cUbuntu下的命令进行编译.

c getchar

70
推荐指数
6
解决办法
11万
查看次数

如何从C运行外部程序并解析其输出?

我有一个实用程序可以输出游戏所需的文件列表.如何在C程序中运行该实用程序并获取其输出,以便我可以在同一程序中对其执行操作?

更新:很好地呼吁缺乏信息.该实用程序吐出一系列字符串,这应该是完全可移植的Mac/Windows/Linux.请注意,我正在寻找一种程序化的方法来执行该实用程序并保留其输出(转到stdout).

c external-process

59
推荐指数
4
解决办法
6万
查看次数

如何从stdin中读取一个字符而不必输入?

我想运行一个在stdin上阻塞的可执行文件,当按下一个键时,Enter不必按下即可立即打印相同的字符.

如何从stdin中读取一个字符而不必点击Enter?我从这个例子开始:

fn main() {
    println!("Type something!");

    let mut line = String::new();
    let input = std::io::stdin().read_line(&mut line).expect("Failed to read line");

    println!("{}", input);
}
Run Code Online (Sandbox Code Playgroud)

我通过API看起来并试图替换read_line()bytes(),但一切我尝试需要我打Enter的读取发生之前.

这个问题被要求用于C/C++,但似乎没有标准的方法:从标准输入中捕获字符而不等待按下输入

考虑到它在C/C++中并不简单,它在Rust中可能不可行.

rust

24
推荐指数
3
解决办法
8461
查看次数

shell脚本响应keypress

我有一个shell脚本,基本上就是这样的

while true; do
    read -r input
    if ["$input" = "a"]; then 
        echo "hello world"           
    fi
done
Run Code Online (Sandbox Code Playgroud)

这一切都很好,很好,但我只是意识到必须点击ENTER在这种情况下提出了一个严重的问题.我需要的是脚本在按下键时响应,而不必按Enter键.

有没有办法在shell脚本中实现此功能?

bash shell input keypress

15
推荐指数
3
解决办法
3万
查看次数

popen隐式声明,即使添加了#include <stdio.h>

这是我的代码的一小部分.

   #include <stdio.h>
   #include <unistd.h>
   #include <stdlib.h>
   #include <time.h>
   #include <sys/stat.h>
   #include <sys/wait.h>
   #include <sys/types.h>
   #include <string.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <arpa/inet.h>
    ...

   FILE * pipe;
    ...

   pipe = popen ("ls /tmp -1", "r");
    ...
   pclose(pipe);
Run Code Online (Sandbox Code Playgroud)
blarg.c:106: warning: implicit declaration of function ‘popen’

blarg.c:106: warning: assignment makes pointer from integer without a cast

blarg.c:112: warning: implicit declaration of function ‘pclose’

blarg.c:118: warning: assignment makes pointer from integer without a cast
Run Code Online (Sandbox Code Playgroud)

我真的不确定.我查了一下popen,它需要的是stdio.h.缺少什么,或者是我的其余代码中的问题(我真的不想显示更多的代码,因为它是一个赋值).

c declaration pipe stdio implicit

14
推荐指数
2
解决办法
8615
查看次数

C中的连续键盘输入

我在C中创建一个控制台应用程序.这是一个游戏,其中角色正在倒下,用户必须按下键盘上的特定键.我不知道如何在不暂停掉落字符的情况下检测用户按下了哪个键.当我使用scanf时,程序等待输入并且一切都暂停.请尽快帮助我!

c c++ console

14
推荐指数
2
解决办法
2万
查看次数

用于Mac/Linux崩溃的Windows getch()相当于

我正在使用getch(),我的应用程序立即崩溃.包括做什么时:

int main()
{
    getch();
}
Run Code Online (Sandbox Code Playgroud)

我找不到链接,但据说问题是它需要关闭缓冲或这些线路上的一些奇怪的东西,我仍然希望cout与跨平台代码一起工作.

我被告知要使用std::cin.get(),但我希望应用程序在按下某个键时退出,而不是当用户输入一个字母或数字然后按回车键退出时.

这有什么功能吗?代码必须在Mac(我的操作系统)和Windows下运行.


链接/编译不是问题 ; 我<curses.h>使用时-lcurses在XCode中包含并链接<conio.h>.

c crash macos getch

11
推荐指数
1
解决办法
2万
查看次数

有没有办法在不按回车键的情况下获得用户输入?

我正在编写一个控制台游戏,(pac-man),我想知道如果没有按下回车键我会得到用户输入.我稍微环顾了一下互联网,我发现了一些东西,_getch()但它显然不再是最新的,没有头文件可以声明它,除非有人建立自己的,我不能做,因为我还是新来的C++.那么我将如何构建可以执行此操作的代码?谢谢

c++ input

11
推荐指数
2
解决办法
7998
查看次数

为什么它没有endl包裹;?

我是一名C++初学者,我刚刚编写了这个简单的程序:

#include <iostream>

using namespace std;

int readNumber()
{
    cout << "Insert a number: ";
    int x;
    cin >> x;
    return x;
}

void writeAnswer(int x)
{
    cout << "The sum is: " << x << endl;
}

int main()
{
    int x = readNumber();
    int y = readNumber();
    int z = x + y;
    writeAnswer(z);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么输出是这样的:

Insert a number: 3
Insert a number: 4
The sum is: 7
Run Code Online (Sandbox Code Playgroud)

而不是像:

Insert a number: 3Insert a number: 4The …
Run Code Online (Sandbox Code Playgroud)

c++

10
推荐指数
1
解决办法
163
查看次数