1

I'm importing data from a text file to a linked list, but fscanf gives weird numbers.

My structure:

struct arac
{
    struct arac *next;
    char marka[15];
    char model[15];
    char paket[15];
    char renk[10];
    int yil;
    char motor[8];
    char sanz[3];
    int km;
    int kayitno;

};

And also this is my adding function:

struct arac *aktar(struct arac *go)
{
    FILE *imptr = fopen("Yenikayit.txt","r");
    if(imptr==NULL) 
    {
        printf("\nDosya bulunamadi.");
        return NULL;
    }
    else
    {
        while(feof(imptr)!=0)
        {
            go=(arac*)malloc(sizeof(arac));
            fscanf(imptr,"\n%d %d %s %s %s %s %d %s %s",go->kayitno, go->km, go->marka, go->model, go->paket, go->renk, go->yil, go->motor, go->sanz);
            printf("\n%d %d %s %s %s %s %d %s %s", go->kayitno, go->km, go->marka, go->model, go->paket, go->renk, go->yil, go->motor, go->sanz);
            go->next=NULL;
            go=go->next;        
        }

    }
}

Embedded the printf for debugging, so it's giving this output, and program crashes:

 947398688 1929405816  S>,│Aæz rs\Berk Ízel\Desktop\otm-final\otm-final.exe sktop\otm-final\otm-final.exe 1818324585 \otm-final.exe al.exe
1207974467 1027954249 \Berk Ízel\Desktop\otm-final\otm-final.exe top\otm-final\otm-final.exe tm-final.exe (x88,│Gùz 1446944
pgngp
  • 1,552
  • 5
  • 16
  • 26

2 Answers2

1

You are giving values of structure members to
fscanf(imptr,"\n%d %d %s %s %s %s %d %s %s",go->kayitno, go->km, go->marka, go->model, go->paket, go->renk, go->yil, go->motor, go->sanz);

You should give pointers to some of them (except those which decay into pointers anyway):

fscanf(imptr,"\n%d %d %s %s %s %s %d %s %s", &(go->kayitno), &(go->km), go->marka, go->model, go->paket, go->renk, &(go->yil), go->motor, go->sanz);

You should also add return statement to the else branch of the if inside aktar().

For verifying the success of the scanf() operation, add the return value to your debug output. Read the spec on the meaning of the return value, it tells you how successful the sanning was.

Then you will notice that scanning for several strings in one line is tricky.
Change to a different reading function to solve that problem.
Using fgets() to get a whole line, followed by parsing the line with e.g. strtok() should be a working concept.

When you succeeded to scan the strings separatly, you will notice that scanning e.g. "Ízel\Desktop\otm-final\otm-final.exe" into a 15-member array of char is doomed and causes writing beyond the target variable and thereby undefined behaviour. If you can define a maximum length, increase the arrays sizes accordingly. If you cannot define a maximum length you have a whole new problem, the one of reading text of unknown length. See other questions on that.

Then there is a problem on a different level of abstraction, it has been mentioned in the comment by pgngp. Quote (in case the valuable comment gets lost...):

Looks like the linked list would not be created correctly because go's previous node is not pointing to go.

A solution to that probably involves using a pointer-to-pointer.

All of this is just supposed to make visible to you that you have several, differently well-hidden, traps in your code. I listed a few, to indicate how complex your root cause might be. I recommend going a few steps back.

The actual answer to your problems you might find by this interesting text:
https://ericlippert.com/2014/03/21/find-a-simpler-problem/
Please understand that it does not mean "give up", or any other impoliteness. It is a method to return your code on the firm ground of something which works and is tested. Then you can start incrementing towards your goal again.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

Ok, i changed function's elements embedding main,

        FILE *fptr = fopen("Yenikayit.txt","r");
        struct arac *go = root;
        //yazdirall(root);
        while(!feof(fptr))
        {
                go=(arac*)malloc(sizeof(arac));
                go->next=NULL;
                fscanf(fptr,"\n%d %d %s %s %s %s %d %s %s", &(go->kayitno), &(go->km), go->marka, go->model, go->paket, go->renk, &(go->yil), go->motor, go->sanz);
                printf("%d %d %s %s %s %s %d %s\n",go->kayitno, go->km, go->marka, go->model, go->paket, go->renk, go->yil, go->motor, go->sanz);
                go=go->next;
        }

While debugging with printf in while loop, it's OK. Printing correct values to output from text.

But when try from root, i getting weird outputs;

    printf("%d %d %s %s %s %s %d %s\n",root->kayitno, root->km, root->marka, root->model, root->paket, root->renk, root->yil, root->motor, root->sanz);