C语言的翻译限制 [英] Translation limit in C

查看:98
本文介绍了C语言的翻译限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过scanf捕获用户的输入:

I am attempting to capture input from the user via scanf:

char numStrings[5000];   
printf("Enter string of numbers:\n\n"); 
scanf("%s", numStrings);

但是,输入的字符串长度为5000个字符。 c99中的翻译限制为4095个字符。我是否需要指示用户将输入减半,还是有我无法想到的更好的解决方法?

However, the length of the string that is inputted is 5000 characters. The translation limit in c99 is 4095 characters. Do I need to instruct the user to break their input in half or is there a better work around that I cannot think of?

推荐答案


我是否需要指示用户将输入减半,还是有我无法想到的更好的解决方法?

Do I need to instruct the user to break their input in half or is there a better work around that I cannot think of?

就给定的代码而言,如果输入字长于4999字节,则可能会导致缓冲区溢出。是的,明智的做法是让某人(例如,用户或下一个维护此代码的人)知道最大长度。可以使用以下代码截断输入,这是很好的: scanf(%4999s%* [^ \n],numStrings); ...在这种情况下,%* [^ \n] 指令执行截断。

As far as the code you've given goes, if the input word is longer than 4999 bytes then you can expect a buffer overflow. Yes, it would be wise to let someone (e.g. the user, or the guy who maintains this code next) know that's the maximum length. It's nice that you can truncate the input by using code like this: scanf("%4999s" "%*[^ \n]", numStrings);... The %*[^ \n] directive performs the truncation, in this case.

更好的是,如果您可以在用户溢出缓冲区时让用户知道,但是 scanf 并不容易。如果您可以使用动态分配,那么甚至更好(对于用户而言)。

It'd be nicer yet if you can let the user know at the time that they overflow the buffer, but scanf doesn't make that an easy task. What would be even nicer (for the user, I mean) is if you could use dynamic allocation.

Ahh,动态调整输入大小的问题。如果可以避免,那就避免它。避免此问题的一种常见方法是要求输入 argv 的形式,而不是 stdin 的形式...但是并非总是可能,有用或可行的。

Ahh, the problem of dynamically sized input. If it can be avoided, then avoid it. One common method to avoid this problem is to require input in the form of argv, rather than stdin... but that's not always possible, useful or feasible.

scanf 并没有使这个问题特别容易解决。实际上,如果%s 以类似于 fgets

scanf doesn't make this problem a particularly easy one to solve; in fact, it'd be much easier to solve if there were a similar functionality provided by %s in the form of an interface similar to fgets.

在没有其他条件的情况下,这是我在此答案,适用于读取(并同时分配)单词的目的,其过程与%s ,而不是与 fgets 后面类似的过程中的 lines 。如果您想进一步了解其背后的灵感,请随时阅读该答案。

Without further adieu, here's an adaptation of the code I wrote in this answer, adapted for the purpose of reading (and simultaneously allocating) words in a similar procedure to that behind %s, rather than lines in a similar procedure to that behind fgets. Feel free to read that answer if you would like to know more about the inspiration behind it.

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

char *get_dynamic_word(FILE *f) {
    size_t bytes_read = 0;
    char *bytes = NULL;
    int c;
    do {
        c = fgetc(f);
    } while (c >= 0 && isspace(c));
    do {
        if ((bytes_read & (bytes_read + 1)) == 0) {
            void *temp = realloc(bytes, bytes_read * 2 + 1);
            if (temp == NULL) {
                free(bytes);
                return NULL;
            }
            bytes = temp;
        }

        bytes[bytes_read] = c >= 0 && !isspace(c)
                            ? c
                            : '\0';
        c = fgetc(f);
    } while (bytes[bytes_read++]);
    if (c >= 0) {
        ungetc(c, f);
    }
    return bytes;
}

这篇关于C语言的翻译限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆