本文介绍了Ç - scanf()的VS得到()VS与fgets()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做转换字符的字符串(假设数字输入)转换为整数的一个相当简单的程序。

I've been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer.

我完成后,我注意到一些非常奇特的错误,我不能回答大多是因我有限的知识,如何使用 scanf()的获得()与fgets()函数的工作。 (我也读了很多文学虽然。)

After I was done, I noticed some very peculiar "bugs" that I can't answer, mostly because of my limited knowledge of how the scanf(), gets() and fgets() functions work. (I did read a lot of literature though.)

因此​​,没有写太多的文字,这里的程序的code:

So without writing too much text, here's the code of the program:

#include <stdio.h>

#define MAX 100

int CharToInt(const char *);

int main()
{
    char str[MAX];

    printf(" Enter some numbers (no spaces): ");
    gets(str);
//  fgets(str, sizeof(str), stdin);
//  scanf("%s", str);

    printf(" Entered number is: %d\n", CharToInt(str));

    return 0;
}

int CharToInt(const char *s)
{
    int i, result, temp;

    result = 0;
    i = 0;

    while(*(s+i) != '\0')
    {
        temp = *(s+i) & 15;
        result = (temp + result) * 10;
        i++;
    }

    return result / 10;
}

因此​​,这里是我一直有这个问题。首先,当使用获得()函数,程序完美的作品。

So here's the problem I've been having. First, when using gets() function, the program works perfectly.

二,使用时与fgets(),结果是略有错误的,因为显然与fgets()函数读取换行符(ASCII值10)字符最后它搞砸了的结果。

Second, when using fgets(), the result is slightly wrong because apparently fgets() function reads newline (ASCII value 10) character last which screws up the result.

三,当使用 scanf()的函数,结果是完全错误的,因为第一个字符显然具有-52 ASCII值。对于这一点,我没有解释。

Third, when using scanf() function, the result is completely wrong because first character apparently has a -52 ASCII value. For this, I have no explanation.

现在我知道,获得()不鼓励使用,所以我想知道如果我可以使用与fgets()这里,所以它不读取(或忽略)换行符。
此外,什么是该计划的成交与 scanf()的功能?

Now I know that gets() is discouraged to use, so I would like to know if I can use fgets() here so it doesn't read (or ignores) newline character.Also, what's the deal with the scanf() function in this program?

推荐答案


  • 从不的使用获得。它提供了针对缓冲区溢出漏洞没有保护(也就是你不能告诉它你传递给它的缓冲区有多大,所以它不能prevent进入行比缓冲区更大,重挫内存的用户)。

  • Never use gets. It offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory).

    避免使用。如果不小心使用,它可以有相同的缓冲区溢出问题,获得。甚至忽略了,的其他问题。

    Avoid using scanf. If not used carefully, it can have the same buffer overflow problems as gets. Even ignoring that, it has other problems that make it hard to use correctly.

    一般来说,你应该使用与fgets 来代替,虽然它有时不方便(你必须剥去换行,你必须提前确定一个缓冲区的大小,然后你必须弄清楚如何处理太长&ndash的线路做;你让你阅读部分,,放弃了整个事情,动态增加缓冲,然后再试一次等​​)。有可用的一些不规范的函数为你做(例如:函数getline 对POSIX系统这个动态分配的功能)。需要注意的是 ggets 获得样,它去除尾随换行对你的语义。

    Generally you should use fgets instead, although it's sometimes inconvenient (you have to strip the newline, you must determine a buffer size ahead of time, and then you must figure out what to do with lines that are too long–do you keep the part you read and discard the excess, discard the whole thing, dynamically grow the buffer and try again, etc.). There are some non-standard functions available that do this dynamic allocation for you (e.g. getline on POSIX systems, Chuck Falconer's public domain ggets function). Note that ggets has gets-like semantics in that it strips a trailing newline for you.

    这篇关于Ç - scanf()的VS得到()VS与fgets()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:02