为什么这两个方法返回不同的东西?

发布于 2024-11-15 02:17:22 字数 587 浏览 2 评论 0原文

所以...我试图制作自己的简单键盘记录器,这适用于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-11-22 02:17:22

可能是因为双击时没有输入流,或者立即为空。在这些情况下,fgetc 将连续返回 EOF。我不能肯定地说,但它解释了你所看到的症状。

您需要将 inputEOF 进行比较,以查看是否已找到流的末尾,因为在这种情况下,您将永远没有机会输入 j。尝试将:更改

if (input==*"j")

为:(

if ((input == 'j') || (input == EOF))

您会注意到我也将相当...不寻常的 *"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 return EOF continously. I can't say that for sure but it explains the symptoms you're seeing.

You need to compare input against EOF to see if the end of the stream has been found because, in that circumstance, you'll never get the chance to input j. Try changing:

if (input==*"j")

to:

if ((input == 'j') || (input == EOF))

(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 an int since it has to represent every possible character plus EOF.

寂寞花火° 2024-11-22 02:17:22
char input=fgetc(stdin);

请注意,fgetc() 需要一个 int(嗯,比 char 更大的东西 - int 是惯例) 获取其返回值:除了 char 可能采用的任何值之外,EOF 是一个可能的返回值。

if (输入==*"j")

哎呀,这太尴尬了。 :) 字符比较(如果在本例中可以的话)看起来像:

if (input == 'j')

了解 '' 字符和 "" 字符串之间的区别对于成为一个好的 '' 字符和 "" 字符串至关重要href="/questions/tagged/c" class="post-tag" title="显示标记为“c”的问题" rel="tag">c 程序员。在其他脚本中出现自由格式的 'string' "string""""string""" 行为之后,可能会感觉有些生硬语言,但事情就是这样。

通常,这些类型的程序是用不同的布局编写的:

int c;
while((c = getchar()) != EOF) {
    /* do something with c */
}

将赋值测试放在while的条件下,一开始可能会感觉很奇怪,但这是惯用的。 (我非常怀念禁止这种行为的语言。)

char input=fgetc(stdin);

Please note that fgetc() requires an int (well, something larger than a char -- int is customary) for its return value: EOF is a possible return value in addition to any of the values that char might take.

if (input==*"j")

Yikes, this is awkward. :) Character comparison (if that were okay in this case) would look like:

if (input == 'j')

Knowing the difference between a '' character and "" string are vital to being a good 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:

int c;
while((c = getchar()) != EOF) {
    /* do something with c */
}

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文