0

hello guys i made a program which inserts into 3 linked lists of struct (linked list) , inserting the name , the path and the duration as you can see in the code , but when im running the project it stucks in this line : fgets(n3->frame->name, 19, stdin); how can i fix that ?

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

struct Frame
{
    char            *name;
    unsigned int    duration;
    char            *path;  // may change to FILE*
}*p , *p1,*p2;

typedef struct Frame frame_t;

struct Link
{
    frame_t *frame;
    struct Link *next;
}*n , *n1 ,*n2;

typedef struct Link link_t;

struct node *insert(struct node *n3);

int main()
{

    printf("1\n");
    insert(n);


    printf("1\n");

}

struct node *insert(struct Link *n3)
{
    while (n3 != NULL)
    {
        n3 = n3->next;

    }
    if (n3 == NULL)
    {


        n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
        fgets(n3->frame->name, 19, stdin);
        n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
        fgets(n3->frame->path, 100, stdin);
        n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
        scanf("%d",&n3->frame->duration);
    }


}

1 Answers1

0

Some documentation for fgets says:

This function reads as much of a line from a file as possible, stopping when the buffer is full (maxlength-1 characters), an end-of-line is detected, or EOF or an error is detected. It then stores a NULL to terminate the string.

The code above does not compile (missing some includes and typedefs?), so I cannot execute it myself, but are you doing things like making sure to hit "enter" after typing in your input?

If you are, I believe you are about to have a whole host of other problems. One example of how to use fgets is in another answer, and there are suspicious things with the above code like allocating memory enough for type struct Frame but casting it to a separate type and assigning to a third (struct Link). I'd suggest adding the -Wall flag to your compilation step if using gcc and generally paying attention to warnings.

Community
  • 1
  • 1
Del
  • 397
  • 2
  • 9