fgets在字符串中生成随机字符

时间:2016-08-22 13:54:41

标签: c

我在C中编写一个简单的数学函数阅读器,并使用gcc-5进行编译。 当我运行以下代码时

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


int read_input(char* func)
{
    printf("Please enter the function: ");
    if (fgets(func, sizeof func, stdin)) {
        printf("The function is %s.\n",func);
    }
    return 0;
}

int main () 
{
    char func[64];
    read_input(func);
    for(int i = 0; i < sizeof(func); i++) {
        printf("Char %d in func is %c\n", i, func[i]);
    }
    return 0;
}

我得到了

Char 0 in func is h
Char 1 in func is e
Char 2 in func is l
Char 3 in func is l
Char 4 in func is o
Char 5 in func is 

Char 6 in func is 
Char 7 in func is 
Char 8 in func is 
Char 9 in func is 
Char 10 in func is 
Char 11 in func is 
Char 12 in func is 
Char 13 in func is 
Char 14 in func is 
Char 15 in func is 
Char 16 in func is 
Char 17 in func is 
Char 18 in func is 
Char 19 in func is 
Char 20 in func is 
Char 21 in func is 
Char 22 in func is 
Char 23 in func is 
Char 24 in func is �
Char 25 in func is 
Char 26 in func is @
Char 27 in func is 
Char 28 in func is 
Char 29 in func is 
Char 30 in func is 
Char 31 in func is 
Char 32 in func is 
Char 33 in func is 
Char 34 in func is 
Char 35 in func is 
Char 36 in func is 
Char 37 in func is 
Char 38 in func is 
Char 39 in func is 
Char 40 in func is 
Char 41 in func is 
Char 42 in func is 
Char 43 in func is 
Char 44 in func is 
Char 45 in func is 
Char 46 in func is 
Char 47 in func is 
Char 48 in func is �
Char 49 in func is 
Char 50 in func is @
Char 51 in func is 
Char 52 in func is 
Char 53 in func is 
Char 54 in func is 
Char 55 in func is 
Char 56 in func is  
Char 57 in func is 
Char 58 in func is @
Char 59 in func is 
Char 60 in func is 
Char 61 in func is 
Char 62 in func is 
Char 63 in func is 

为什么我会得到一个新的角色,我怎么能在我的fgets中得到它?还有什么是我得到的那些奇怪的角色?

4 个答案:

答案 0 :(得分:4)

if (fgets(func, sizeof func, stdin))

问题是数组在传递给函数时会衰减为指向其第一个元素的指针。您正在传递char数组,然后该数组将衰减为指向char的指针。因此,使用sizeof语句,您只需获取当前计算机上指针的大小。解决方案是将元素的数量作为参数传递给read_input,并将其用作fgets的第二个参数。或者,使用strlen

答案 1 :(得分:3)

您正在使用sizeof来确定数组的大小,并且它不会返回输入的字符数,更改您用于打印字符的代码:

int size = strlen(func);
for(int i = 0; i < size; i++) {
    printf("Char %d in func is %c\n", i, func[i]);
}

你应该将数组的大小作为参数传递给读取输入的函数,即

void read_input(char* func, int size)
{
    printf("Please enter the function: ");
    if (fgets(func, size, stdin)) {
        printf("The function is %s.\n",func);
    }
}

正如您所看到的,我还更改了函数的返回类型,因为在没有需要时函数返回int是没用的。而是使用void

答案 2 :(得分:1)

问题在于sizeof func

这将是指针的大小。

正确使用strlen(func)

我的输出是strlen:

Please enter the function: hello
The function is hello.
Char 0 in func is h
Char 1 in func is e
Char 2 in func is l
Char 3 in func is l
Char 4 in func is o

答案 3 :(得分:0)

问题是sizeof func在函数内部和主函数中是不同的。在功能中它是:

sizeof(char*)     // Will probably give 8 (or 4)

主要是:

sizerof(char[64])  // Will give 64

所以你只允许输入8(或4)个字符,但你打印64。

你需要这样做:

1)添加size作为函数参数,并在调用函数时使用sizeof func

2)将for循环更改为使用strlen(func)而不是sizeof func,以便只打印出有效字符。

要删除func末尾的换行符,请参阅https://stackoverflow.com/a/28462221/4386427