C - 使用 fgets() 填充通过循环迭代的结构数组。如何在同一行上打印这些值?

标签 c arrays structure fgets

我在 C 语言中闲逛,试图了解它是如何工作的,但遇到了一个问题。我有一个用两个字符数组变量定义的结构。我使用键盘输入中的 fgets() 填充它们。但是,当我打印时,输出如下所示:

Gibson
Les Paul
Fender
Stratocaster

当我真的希望它看起来像这样时:

Gibson Les Paul
Fender Stratocaster

当使用 scanf 而不是 fgets 时,我可以很好地完成此任务,但我想我会看看我是否能理解为什么会发生这种情况,因为我是 C 新手。

这是我的代码:

#include <stdio.h>

typedef struct Guitars
{ 
    char brand[10];
    char model[10];
} input;

void input_data(struct Guitars input[10])
{
    for(int i=0; i<2; i++)
    {
        printf("Please enter the brand: ");
        fgets(input[i].brand, 10, stdin);
        printf("Please enter the model: ");
        fgets(input[i].model, 10, stdin);
    }
}

void print_array(struct Guitars input[10])
{
    for(int i=0; i<2; i++) 
    {
        printf("%s%s", input[i].brand, &input[i].model);
    }   
}

int main(void) {
    struct Guitars input[10];
    input_data(input);
    print_array(input);
}

感谢您的帮助!

最佳答案

太大,无法容纳单个评论。

确保 .brand.model 都不包含换行符(但不要使用 gets():请参阅 Why gets() is too dangerous to be used — ever! ) 。请记住,fgets() 包含换行符(如果它适合缓冲区)。您可以通过以下方式删除它:

if (fgets(buffer, sizeof(buffer), stdin) != 0)
{
    buffer[strcspn(buffer, "\n")] = '\0';
    …
}

然后使用:

printf("%s %s\n", input[i].brand, input[i].model);

注意 %s 转换规范和末尾换行符之间的空格。如果您愿意,您可以使用冒号、破折号或类似的内容来更清晰地区分品牌和型号。

原始评论结束。

另请注意,Stratocaster 不适合 10 个数组。您的结构需要更大的数组来存储示例数据。您还需要考虑在数组中创建了多少条目 - 您硬连线了 2 个条目,这有点限制。

将这些更改放在一起会生成如下代码:

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

struct Guitars
{
    char brand[20];
    char model[20];
};

static int input_data(int max_entries, struct Guitars input[])
{
    int i;
    for (i = 0; i < max_entries; i++)
    {
        printf("Please enter the brand: ");
        if (fgets(input[i].brand, sizeof(input[i].brand), stdin) == 0)
        {
            fprintf(stderr, "Unexpected EOF\n");
            break;
        }
        input[i].brand[strcspn(input[i].brand, "\n")] = '\0';
        printf("Please enter the model: ");
        if (fgets(input[i].model, sizeof(input[i].model), stdin) == 0)
        {
            fprintf(stderr, "Unexpected EOF\n");
            break;
        }
        input[i].model[strcspn(input[i].model, "\n")] = '\0';
    }
    return i;
}

static void print_array(int num_entries, struct Guitars input[])
{
    for (int i = 0; i < num_entries; i++)
    {
        printf("%s %s\n", input[i].brand, input[i].model);
    }
}

int main(void)
{
    struct Guitars input[10];
    int n = input_data(10, input);
    print_array(n, input);
    return 0;
}

示例运行:

Please enter the brand: Gibson
Please enter the model: Les Paul
Please enter the brand: Fender
Please enter the model: Stratocaster
Please enter the brand: Unexpected EOF
Gibson Les Paul
Fender Stratocaster

您可以很容易地认为“意外的 EOF”消息对于第一个(品牌)输入不利;对于第二个(模型)输入来说更有说服力。可以轻松修改代码以满足您的需求。

关于C - 使用 fgets() 填充通过循环迭代的结构数组。如何在同一行上打印这些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972143/

相关文章:

c - 当事件发生时如何启动多个线程?

java - 解码错误时未找到类。可使用 byteArray、byte[] 进行解析

时钟守护者功能。程序 'works' 但值打印顺序错误?

c++ - 类中的结构定义

c++ - 如何使用 "new"而不是 malloc 分配内存?

c - 当我们在 C 中为链表定义结构时,为什么我们不会出错

C - 数组内容交换

c - C语言中的时间触发器

c - do-while 循环只迭代一次月供

c - 二维指针数组 C