简体   繁体   English

将stdin设置为C中的文件时如何从键盘读取

[英]How to read from keyboard when stdin is set to a file in C

I think this is simple, but not for me obviously! 我认为这很简单,但对我来说显然不是!

I have a console-application. 我有一个控制台应用程序。 I need to read input from the keyboard, but stdin has been redirected to a file. 我需要从键盘读取输入,但是stdin已被重定向到文件。 So how do I create a FILE-Handle that points at the keyboard-stream which i can use with fgets etc.? 那么,如何创建指向可与fgets等配合使用的键盘流的FILE-Handle?

I found out that ttyname(0) seems to be what i look for in a POSIX-environment, which I don't have here. 我发现ttyname(0)似乎是我在POSIX环境中需要的东西,而我在这里没有。 I'm in a Windows-system with standard Visual Studio compiler. 我在带有标准Visual Studio编译器的Windows系统中。

Any ideas? 有任何想法吗? Thank you in advance. 先感谢您。

Pulling from the dim dark days of DOS programming here: try opening "CON:" (Console), a reserved word. 在这里摆脱DOS编程的黑暗时光:尝试打开保留字“ CON:”(控制台)。 Hopefully it will open the same way in Windows. 希望它将在Windows中以相同的方式打开。 The colon may or may not be required. 可能需要也可能不需要冒号。 Both "dir >con:" and "dir >con" still work in command prompt. “ dir> con:”和“ dir> con”都仍在命令提示符下工作。

Also, be sure to use something from the setbuf() family on the output handle to avoid buffering... it's not supposed to buffer terminal I/O, but it never hurts to be sure. 另外,请确保在输出句柄上使用setbuf()系列中的某些内容以避免缓冲...本来不应该缓冲终端I / O,但可以肯定地说,这不会有任何伤害。

Again, not sure, but I suspect opening separate FILE *conin, *conout for output and one for input may help if you seem to have troubles with one handle doing both input and output. 同样,不确定,但是如果您似乎在用一个句柄同时进行输入和输出时遇到麻烦,我怀疑打开单独的FILE *conin, *conout用于输出和一个用于输入可能会有所帮助。

There's no easy/portable way to tell if a keyboard exists (your application may be being run from a terminal emulator from a serial port, a telnet session or anything else). 没有简单/便携式的方法来判断键盘是否存在(您的应用程序可能正在通过串行端口,telnet会话或其他任何方式从终端仿真器运行)。 If a keyboard actually does exist (including a picture of a keyboard on a touch screen), then you can't really tell how many layers of software the keystrokes need to pass through before they get to your application (eg keystrokes might go from a keyboard driver to an input method editor to a GUI to a shell to your application). 如果确实存在一个键盘(包括触摸屏上的键盘图片),则您无法真正判断出击键在进入您的应用程序之前需要经过多少层软件(例如,击键可能来自某个键盘)。键盘驱动程序到输入法编辑器,GUI到应用程序的外壳)。 This means that attempting to get keystrokes directly from a keyboard driver or something is a bad idea that will fail in almost all cases. 这意味着尝试直接从键盘驱动程序或其他工具获取击键是一个坏主意,几乎在所有情况下都会失败。

The best way to solve your problem is to find out which series of design failures led to STDIN being redirected in the first place. 解决问题的最佳方法是首先找出导致STDIN重定向的是哪一系列设计故障。

For example; 例如; maybe the application should've had a command line option to read some data from a file; 也许应用程序应该具有命令行选项才能从文件中读取一些数据; so that the application can get some data from the file and some from STDIN (and get all data from STDIN if the command line option isn't present). 这样应用程序就可以从文件中获取一些数据,并从STDIN中获取一些数据(如果不存在命令行选项,则可以从STDIN中获取所有数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM