2

I'm trying to make a program that would read a linked list and output all of the info that it has read. My problem is that I can't simply output. There is some problem that I can't find.

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


struct sarasas
{
    char *reiksme;
    struct sarasas *kitas;
};



int main()
{
    struct sarasas *sarasasPtr, *pradz, *pab, *elem;

    pradz = NULL;
    pab = NULL;

    FILE *duomPtr;
    printf("Iveskite duomenu failo pavadinima: ");
    char failas[255];
    scanf("%s", failas);
    duomPtr = fopen(failas, "r");

    if(duomPtr == NULL)
    {
        printf("Toks duomenu failas neegzistuoja \n");
        exit(0);
    }

    int k = 0;
    char paimtaReiksme[255];

    while(fscanf(duomPtr, "%s", paimtaReiksme) != EOF)
    {
        if(k == 0)
        {
            sarasasPtr = (struct sarasas*)malloc (sizeof (struct sarasas));
            sarasasPtr->reiksme = paimtaReiksme;
            sarasasPtr->kitas = NULL;
            pradz = sarasasPtr;
            pab = sarasasPtr;
        }

        else
        {
            sarasasPtr = (struct sarasas*)malloc (sizeof (struct sarasas));
            sarasasPtr->reiksme = paimtaReiksme;
            sarasasPtr->kitas = NULL;
            pab->kitas = sarasasPtr;
            pab = sarasasPtr;
        }
        k++;
    }

    if(pradz == NULL &&  pab == NULL)
    {
        printf("Tuscia\n");
        exit(0);
    }

    FILE *rptr;
    printf("Iveskite rezultatu failo pavadinima: ");
    char failas2[255];
    scanf("%s", failas2);
    rptr = fopen(failas2, "w");

    while(sarasasPtr->kitas != NULL)
    {
        fprintf(rptr, "%s", sarasasPtr->reiksme);
    }


    return 0;
}
Cherubim
  • 5,287
  • 3
  • 20
  • 37
Adomas202
  • 99
  • 1
  • 9

1 Answers1

2

You have an infinite loop in your code.

while(sarasasPtr->kitas != NULL)
{
    fprintf(rptr, "%s", sarasasPtr->reiksme);
}

Here in the above while loop, you are trying to print the same element over and over again and thus you end up in an infinite loop.instead, you must change the pointer to next element after each and every iteration. You can try something like this:

while(sarasasPtr != NULL) //check whether pointer points to NULL
{
    fprintf(rptr, "%s", sarasasPtr->reiksme);
    sarasasPtr = sarasasPtr->kitas; //make pointer point to next element
}

additionally, you need not cast the the return value of malloc : Here's why(click)

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • I have found that this program only prints to the file one word. Maybe you know why is that so? @Cherubim ? – Adomas202 Dec 17 '16 at 20:23
  • is it the last word that is only getting printed. the problem is that you are trying to point the same `char array[]` i.e, `paimtaReiksme` over and over again to the `sarasasPtr->reiksme`. try using `strcpy()` function – Cherubim Dec 17 '16 at 20:34
  • 1: include `string.h` header file | 2: use `strcpy(sarasasPtr->reiksme, paimtaReiksme);` instead of `sarasasPtr->reiksme = paimtaReiksme;` everywhere – Cherubim Dec 17 '16 at 20:37
  • And don't forget to allocate memory for ` sarasPtr->reiksme` before using strcpy.... `saradPtr->reiksme = malloc(strlen(paimtaReiksme))`.. @AdomasArabella – Cherubim Dec 18 '16 at 08:46