关于popen打开指令无法及时得到屏幕输出的问题

yupengchen1985 2013-04-27 02:35:46
本人想在android系统中设计一个server,用于监控按键的按下事件,于是想到了使用popen调用getevent /dev/input/event3 这个指令,并且根据指令的控制台输出获得按键是否被按下,如下代码:
FILE* flie = popen("getevent /dev/input/event3", "r");
if(fgets(line, 32, flie) != NULL)
来获得指令的输出。但是这个办法却不能及时获得指令的输出,要等到指令的输出buf到一定程度的时候才能
fgets出来,不能及时反应按键的按下情况。我自己也试了fflush刷新file,但还是没有任何作用。奇怪了,我在adb shell执行getevent /dev/input/event3这个命令的时候会及时获得指令的输出结果,各位亲,谁做过这方便的东东,提个建议啊……分不够可以加。。。。。。。

...全文
527 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yupengchen1985 2013-05-03
  • 打赏
  • 举报
回复
谢谢楼上,我直接移植getevent相关代码到我的项目中,这样就不需要popen了。谢谢了 。
赵4老师 2013-05-02
  • 打赏
  • 举报
回复
如果在linux或windows下可以,在android下不行,我猜是android不支持。
yupengchen1985 2013-05-02
  • 打赏
  • 举报
回复
就这样沉下去了。CSDN真没有用
yupengchen1985 2013-04-27
  • 打赏
  • 举报
回复
回楼上 我试过了这样的做法,还是没有效果呢
赵4老师 2013-04-27
  • 打赏
  • 举报
回复
setvbuf Controls stream buffering and buffer size. int setvbuf( FILE *stream, char *buffer, int mode, size_t size ); Routine Required Header Compatibility setvbuf <stdio.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value setvbuf returns 0 if successful, or a nonzero value if an illegal type or buffer size is specified. Parameters stream Pointer to FILE structure buffer User-allocated buffer mode Mode of buffering size Buffer size in bytes. Allowable range: 2 < size < 32768. Internally, the value supplied for size is rounded down to the nearest multiple of 2. Remarks The setvbuf function allows the program to control both buffering and buffer size for stream. stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes. The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored. Values for mode and their meanings are: _IOFBF Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used. _IOLBF With MS-DOS, the same as _IOFBF. _IONBF No buffer is used, regardless of buffer or size. Example /* SETVBUF.C: This program opens two streams: stream1 * and stream2. It then uses setvbuf to give stream1 a * user-defined buffer of 1024 bytes and stream2 no buffer. */ #include <stdio.h> void main( void ) { char buf[1024]; FILE *stream1, *stream2; if( ((stream1 = fopen( "data1", "a" )) != NULL) && ((stream2 = fopen( "data2", "w" )) != NULL) ) { if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 ) printf( "Incorrect type or size of buffer for stream1\n" ); else printf( "'stream1' now has a buffer of 1024 bytes\n" ); if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 ) printf( "Incorrect type or size of buffer for stream2\n" ); else printf( "'stream2' now has no buffer\n" ); _fcloseall(); } } Output 'stream1' now has a buffer of 1024 bytes 'stream2' now has no buffer Stream I/O Routines See Also fclose, fflush, fopen, setbuf

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧