Unhandled exception at 0x0F64F6E0 (ucrtbased.dll) in Assignment_SDD_Proj.exe: 0xC0000005: Access violation reading location 0x00000000

1

So I have the following code:

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

#define LINESIZE 128
#define DIM 100;

struct Depozit {
    int id;
    char* denumire=NULL;
    char* localitate=NULL;
    float suprafata;
    float capacitate_stocare;
    int numar_angajati;
};

struct Nod {
    Depozit* info;
    Nod* next, *prev;
};

struct ListaDubla {
    Nod* prim, *ultim;
};

ListaDubla insertNodeInLD(ListaDubla lst, Depozit* dep) {
    struct Nod* curent;
    Nod* nodnou = (Nod*)malloc(sizeof(Nod));

    nodnou->info = dep;
    nodnou->prev = nodnou->next = NULL;

    if (lst.prim == NULL)
        lst.prim = nodnou;

    else if (lst.prim->info->id > nodnou->info->id) {
        nodnou->next = lst.prim;
        nodnou->next->prev = nodnou;
        lst.prim = nodnou;
    }
    else {
        curent = lst.prim;

        while (curent->next != NULL && curent->next->info->id < nodnou->info->id)
            curent = curent->next;

        nodnou->next = curent->next;

        if (curent->next != NULL)
            nodnou->next->prev = nodnou;
        else
            lst.ultim = nodnou;

        curent->next = nodnou;
        nodnou->prev = curent;
    }
    return lst;
}


void printListD(ListaDubla lst) {
    Nod* temp = lst.prim;
    while (temp) {
        printf("Depozit %d, %s, %s, %f, %f, %d\n", temp->info->id, temp->info->denumire,
            temp->info->localitate, temp->info->suprafata, temp->info->capacitate_stocare, temp->info->numar_angajati);

        temp = temp->next;
    }
}

void main() {

    ListaDubla LD;
    LD.prim = LD.ultim = NULL;
    Depozit* pdep =NULL;



    FILE* F;
    F = fopen("Depozit.txt", "r");

    char* token, file_buf[LINESIZE], sep_list[] = ",";
    while (fgets(file_buf, sizeof(file_buf), F)) {



        token = strtok(file_buf, sep_list);
        pdep = (Depozit*)malloc(sizeof(Depozit));
        pdep->id = atoi(token);

        token = strtok(NULL, sep_list);
        pdep->denumire = (char*)malloc((strlen(token) + 1) * sizeof(char));
        strcpy(pdep->denumire, token);

        token = strtok(NULL, sep_list);
        pdep->localitate = (char*)malloc((strlen(token) + 1) * sizeof(char));
        strcpy(pdep->localitate, token);

        token = strtok(NULL, sep_list);
        pdep->suprafata = (float)atof(token);

        token = strtok(NULL, sep_list);
        pdep->capacitate_stocare = (float)atoi(token);

        token = strtok(NULL, sep_list);
        pdep->numar_angajati = atoi(token);
        token = strtok(NULL, sep_list);


        LD = insertNodeInLD(LD, pdep);

    }

    printf("Lista dupa creare, ordonata: \n");
    printListD(LD);


    fclose(F);
}

I'm pretty new to this. I'm trying to read this text file but it doesn't work and I think I got stuck:

4901,Amazon,Bucuresti,327,2040,5000
4902,Am,Constanta,202,2700,6000
4903,zon,Tulcea,190,1000,7000
4904,Ama,Brasov,280,2070,2000
4905,A,Sibiu,312,2050,1000
4906,Akl,Sinaia,358,2040,4000
4907,Ank,Timisoara,400,300,2000

A little help would be warmly welcomed.

c
asked on Stack Overflow May 15, 2020 by Catherine Spiller • edited May 15, 2020 by cigien

1 Answer

0

I tried your program under linux + macos, and it ran fine. I know MS-DOS had problems properly handling line terminators, and I think Windows inherited it; so I changed the second line of your file to omit a comma, and could make your program cause an exception. Following this, if you changed the strtok lines to:

if ((token = strtok(file_buf, sep_list)) == NULL) continue;
pdep = (Depozit*)malloc(sizeof(Depozit));
pdep->id = atoi(token);

if ((token = strtok(NULL, sep_list)) == NULL) continue;
pdep->denumire = (char*)malloc((strlen(token) + 1) * sizeof(char));
strcpy(pdep->denumire, token);

if ((token = strtok(NULL, sep_list)) == NULL) continue;
pdep->localitate = (char*)malloc((strlen(token) + 1) * sizeof(char));
strcpy(pdep->localitate, token);

if ((token = strtok(NULL, sep_list)) == NULL) continue;
pdep->suprafata = (float)atof(token);

if ((token = strtok(NULL, sep_list)) == NULL) continue;
pdep->capacitate_stocare = (float)atoi(token);

if ((token = strtok(NULL, sep_list)) == NULL) continue;
pdep->numar_angajati = atoi(token);

And your exceptions go away, then you aren't reading the lines you expect to. With a debugger you could step along and see when strtok fails.

answered on Stack Overflow May 15, 2020 by mevets

User contributions licensed under CC BY-SA 3.0