【问题标题】:Linked list in C with fgets()C 中的链表与 fgets()
【发布时间】:2016-04-19 23:24:57
【问题描述】:

我的代码在第二个数据结构中跳过第一个问题时遇到问题。我很确定这是因为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;
}

【问题讨论】:

  • scanf("%f", &amp;node-&gt;networth); --> scanf("%f%*c", &amp;node-&gt;networth); %*c 消耗剩余的换行符。还有if (i == 2) --> if (i == 4) ?
  • 混合fgets 和scanf 总是很糟糕。使用gets 是完全错误的。
  • @BLUEPIXY 抱歉,if 语句应该是 if(I == NumberOfActresses - 1)。使用 %*c 可以解决这个问题,还是我应该使用其他东西?
  • @user3386109 那我应该用什么?
  • 我认为这按预期工作。如果强行说,请确保更好地释放内存(free'ed)。

标签: c data-structures linked-list


【解决方案1】:

混合fgets 和scanf 总是很糟糕。原因是scanf 将在输入流中留下换行符,因此后面的fgets 将读取一个空行。并且使用gets 是just 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;
}

哦,我将 networth 从 float 更改为 double。 float 的精度只有 6 或 7 位,这对于女演员/演员的净资产来说还远远不够。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多