为什么这两个方法返回不同的东西?
所以...我试图制作自己的简单键盘记录器,这适用于在 shell 中键入的内容,但如果我双击可执行文件,它只会将很多这些内容放入文件中:ÿ
我知道现在如果我输入 j 它就会结束;这是为了调试:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
FILE *fp = fopen("log", "w");
if (fp != NULL)
{
int x=0;
while (x==0)
{
char input=fgetc(stdin);
if (input==*"j")
x=1;
else
{
fprintf(fp, "%c\n",input);
}
}
fclose(fp);
}
return 0;
}
So...I was trying to make my own simple keylogger and this works for things typed at the shell, but if I double click the executable file it just puts a lot of these in the file: ÿ
I understand that as of now if I type a j it will end; this is for debugging:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
FILE *fp = fopen("log", "w");
if (fp != NULL)
{
int x=0;
while (x==0)
{
char input=fgetc(stdin);
if (input==*"j")
x=1;
else
{
fprintf(fp, "%c\n",input);
}
}
fclose(fp);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为双击时没有输入流,或者立即为空。在这些情况下,
fgetc
将连续返回EOF
。我不能肯定地说,但它解释了你所看到的症状。您需要将
input
与EOF
进行比较,以查看是否已找到流的末尾,因为在这种情况下,您将永远没有机会输入j。尝试将:更改
为:(
您会注意到我也将相当...不寻常的
*"j"
xonstruct 更改为更简单的'j'
)。fgetc
的返回值也应该是int
,因为它必须表示每个可能的字符加上EOF
。Probably because there's no input stream when you double click, or it's empty straight away. In those conditions,
fgetc
will returnEOF
continously. I can't say that for sure but it explains the symptoms you're seeing.You need to compare
input
againstEOF
to see if the end of the stream has been found because, in that circumstance, you'll never get the chance to inputj
. Try changing:to:
(you'll noticed I've changed the rather ... unusual
*"j"
xonstruct to the simpler'j'
as well).The return value from
fgetc
should also be anint
since it has to represent every possible character plusEOF
.请注意,
fgetc()
需要一个int
(嗯,比char
更大的东西 -int
是惯例) 获取其返回值:除了char
可能采用的任何值之外,EOF
是一个可能的返回值。哎呀,这太尴尬了。 :) 字符比较(如果在本例中可以的话)看起来像:
了解
''
字符和""
字符串之间的区别对于成为一个好的''
字符和""
字符串至关重要href="/questions/tagged/c" class="post-tag" title="显示标记为“c”的问题" rel="tag">c 程序员。在其他脚本中出现自由格式的'string'
"string"
和"""string"""
行为之后,可能会感觉有些生硬语言,但事情就是这样。通常,这些类型的程序是用不同的布局编写的:
将赋值和测试放在
while
的条件下,一开始可能会感觉很奇怪,但这是惯用的。 (我非常怀念禁止这种行为的语言。)Please note that
fgetc()
requires anint
(well, something larger than achar
--int
is customary) for its return value:EOF
is a possible return value in addition to any of the values thatchar
might take.Yikes, this is awkward. :) Character comparison (if that were okay in this case) would look like:
Knowing the difference between a
''
character and""
string are vital to being a good c programmer. It might feel stilted after the free-form'string'
"string"
and"""string"""
sorts of behaviors from other scripting languages, but it's the way it is.Typically, these sorts of programs are written with a different layout:
Putting an assignment and test in the condition of a
while
might feel weird at first, but it is idiomatic. (And I've sorely missed this behavior in languages that forbid it.)