简体   繁体   English

由putchar在最后一个字符后打印的垃圾

[英]Garbage being printed after last character by putchar

My putchar() function is returning garbage after the sum is made. 我的putchar()函数在求和之后返回垃圾。

Here's my code snippet: 这是我的代码片段:

scanf("%d", & keys);
getchar();
while ((c = getchar()) != EOF)
{
    c = c + keys;
    putchar(c);
}
puts("");

If I understand where you are going, your problem (the funny characters at the end) are due to your addition of c = c + keys; 如果我明白你要去哪里,你的问题(最后有趣的人物)是由于你加入了c = c + keys; resulting in a character value greater than 126 (eg the '~' char). 导致字符值大于126 (例如'~' 〜'char)。 For example, if your keys is greater than 4 and you input 'z' , then your resulting c + keys is outside the valid range of ASCII characters. 例如,如果您的keys大于4并且输入'z' ,则生成的c + keys超出了ASCII字符的有效范围。 See ASCII Table and Description 请参见ASCII表和说明

Depending on what you want to do, you can simply use % (modulo) to ensure you always have a valid ASCII character before calling putchar() . 根据你想要做的事情,你可以简单地使用% (modulo)来确保在调用putchar()之前总是有一个有效的ASCII字符。 Something like: 就像是:

    while ((c = getchar()) != EOF) {
        c = (c + keys) % ('~' - ' ' + 1) + ' '; /* use modulo to ensure ASCII */
        putchar (c);
    }

( note: '~' - ' ' + 1 is simply the printable range of ASCII values -- 95 chars - thanks Roland) 注意: '~' - ' ' + 1只是ASCII值的可打印范围 - 95字符 - 感谢Roland)

Putting together a guess at a short example program that appears to be where you are going, you could do: 把一个简短的示例程序放在一个看起来就像你要去的地方,你可以这样做:

#include <stdio.h>

/* simple helper function to remove to end of line */
void empty_line (void)
{
    int c = getchar();

    while (c != '\n' && c != EOF)
        c = getchar();
}

int main (void) {

    int c, keys;

    if (scanf("%d", & keys) != 1 || keys < 0) { /* Validate EVERY Input! */
        fputs ("error: invalid or negative integer input.\n", stderr);
        return 1;
    }
    empty_line();

    while ((c = getchar()) != EOF) {
        c = (c + keys) % ('~' - ' ' + 1) + ' '; /* use modulo to ensure ASCII */
        putchar (c);
    }
    putchar ('\n');     /* not puts, you need 1-char, not string */
}

( note: you must Validate every input, especially when using scanf to perform a conversion to int -- or any other type) 注意:您必须验证每个输入, 尤其是在使用scanf执行转换为int - 或任何其他类型)

Example Use/Output 示例使用/输出

$ ./bin/putcharmod
33
My dog has zero fleas, my cat has none :)
/[aFQIaJCUa\GTQaHNGCUmaO[aECVaJCUaPQPGa{jK

Above, despite having keys = 33 , inputting 'z' results in no funny characters as the total c + keys is reduced to within the printable range of characters. 上面,尽管keys = 33 ,输入'z'不会产生有趣的字符,因为总c + keys减少到可打印的字符范围内。

Of course adjust the scheme to meet whatever your end objective happens to be, but regardless, if you are outputting to stdout with putchar() , you will need to do something similar to make sure what you output is printable. 当然,调整方案以满足您的最终目标,但无论如何,如果您使用putchar()输出到stdout ,您将需要执行类似的操作以确保输出的内容是可打印的。

Let me know if you have further questions. 如果您有其他问题,请与我们联系。

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

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