c - C 中的链表与 fgets()

标签 c data-structures linked-list

我的代码在跳过第二个数据结构中的第一个问题时遇到问题。我很确定这是因为 gets(),但不确定。我想我尝试了 fgets(),但它仍然给我带来了问题。为什么?

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

#define NumberOfActresses 5

typedef struct Actress
{
char *name, *placeofbirth, *haircolor;
int age;
float networth;
struct Actress *next;

} Actress;

void PopulateStruct(Actress *node)
{
node->name = (char *) malloc(sizeof(char) * 50);
node->placeofbirth = (char *) malloc(sizeof(char) * 50);
node->haircolor = (char *) malloc(sizeof(char) * 50);

printf("Please enter the name of the actress/actor: ");
gets(node->name);
printf("Please enter the actress/actor place of birth: ");
gets(node->placeofbirth);
printf("Please enter the actress/actor hair color: ");
gets(node->haircolor);
printf("Please enter the actress/actor age: ");
scanf("%d", &node->age);
printf("Please enter the actress/actor networth: ");
scanf("%f", &node->networth);
}

void DisplayStruct(Actress *head)
{
Actress *crawler;

crawler = head;

while(crawler != NULL)
{
    printf("The name of the actress/actor is: %s\n", crawler->name);
    printf("The place of birth for the actress/actor is: %s\n",       crawler->placeofbirth);
    printf("The hair color of the actress/actor is: %s\n", crawler->haircolor);
    printf("The actress/actor age is: %d\n", crawler->age);
    printf("The networth for the actress/actor is %f\n", crawler->networth);
    crawler = crawler->next;
}
}

int main()
{
int i;
Actress *head = (Actress *) malloc(sizeof(Actress)), *crawler;

crawler = head;

 for (i = 0; i < NumberOfActresses; i++)
    {
    PopulateStruct(crawler);
    if (i == 2)
        crawler->next = NULL;
    else
        crawler->next = malloc(sizeof(Actress));

    crawler = crawler->next;
}

crawler = NULL;

DisplayStruct(head);

return 0;
}

最佳答案

混合使用fgetsscanf总是会产生糟糕的结果。原因是 scanf 会将换行符保留在输入流中,因此后面的 fgets 将读取空行。使用 getsjust plain wrong .

解决方案是始终使用 fgets 读取输入,然后根据需要使用 sscanf 解析输入。对于不需要 sscanf 的情况,即输入是字符串,您可以使用 strcspn 从缓冲区中删除换行符。

int PopulateStruct(Actress *node)
{
    char buffer[100];

    printf("Please enter the name of the actress/actor: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    buffer[strcspn(buffer,"\n")] = '\0';
    if ( (node->name = malloc(strlen(buffer) + 1)) == NULL )
        return 0;
    strcpy( node->name, buffer );

    // ditto for place of birth and hair color

    printf("Please enter the actress/actor age: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    if ( sscanf( buffer, "%d", &node->age ) != 1 )
        return 0;

    printf("Please enter the actress/actor networth: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    if ( sscanf( buffer, "%lf", &node->networth ) != 1 )
        return 0;

    return 1;
}

哦,我将networthfloat更改为doublefloat 的精度只有 6 或 7 位,这对于女 Actor / Actor 的净 Assets 来说还远远不够。

关于c - C 中的链表与 fgets(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731181/

相关文章:

c - 释放 OpenCV IplImage 会使应用程序崩溃?

c - 一棵树在任何级别都有多个 child ,找到两个节点之间的路径

c++ - 链表,在最后插入 C++

c - 链表中该符号的解释

c - 如何取任何整数,例如 10050,并将其输出为 $100.50?

c - rand() 在我的自定义函数中不返回任何值

c - MPI 减少到特定的接收缓冲区地址

javascript - 我遇到过的最奇怪的问题。数组在 push 语句之前获取值

c# - 为什么 List<T>.Add 和 List<T>.Remove 将元素复制到新数组中而不是实际添加和删除?

C++ 函数返回链表