I'm writing a program that should handle some data stored inside a file and it runs perfectly when I run it in debug mode inside codeblocks but crashes whenever I try to make it run differently (either by clicking on the exe or try running the release mode) I tried everyhing and I think there shouldn't be out of bounds accesses or uninitialized pointers this is just an abstract from the code since it stops on the function readCSV, the csv file contains 53 lines and the count rows is able to count them properly also on release, the printfs were ony to understand where it stops executing I hope there are some errors that I can't spot, thanks in advance for the help
typedef struct
{
char* codice;
char* nome;
char* album;
char* artista;
int track;
Durata durata;
Genere genere;
}Brano;
int countRowsCSV(const char* filename)
{ FILE* file=fopen(filename,"r");
char line[200];
int i=0;
if(file==NULL)
return -1;
while(!feof(file))
{if(fgets(line,sizeof(line),file)!=NULL)
i++;
}
fclose(file);
return i;
}
char* getString(char* line)
{
printf("%s",line);
char* a=NULL;
a=(char*)malloc(sizeof(char)*strlen(line));
strcpy(a,line);
return a;
}
void readCSV(Brano* catalogo)
{
FILE* file=fopen("brani.csv","r");
char line[200]="";
int i;
if(file==NULL)
return ;
for (i=0;(!feof(file));i++)
{
if(fgets(line,sizeof(line),file)!=NULL)
{
if(line[strlen(line)-1]!='\0')//I remove the final \n
line[strlen(line)-1]='\0';
printf("%s",line);
catalogo[i].codice=NULL;
catalogo[i].codice=getString(strtok(line,";"));
printf("%s\n",catalogo[i].codice);
catalogo[i].nome=NULL;
catalogo[i].nome=getString(strtok(NULL,";"));
printf("%s\n",catalogo[i].nome);
catalogo[i].album=NULL;
catalogo[i].album=getString(strtok(NULL,";")); //the program often crashes here with i=0
printf("%s\n",catalogo[i].album);
catalogo[i].artista=NULL;
catalogo[i].artista=getString(strtok(NULL,";"));
printf("%s\n",catalogo[i].artista);
catalogo[i].track=atoi(strtok(NULL,";"));
printf("%d\n",catalogo[i].track);
catalogo[i].durata.minuti=atoi(strtok(NULL,";"));
catalogo[i].durata.secondi=atoi(strtok(NULL,";"));
catalogo[i].genere=stringToGenere(strtok(NULL,";"));
}
}
fclose(file);
}
int main(void) {
int scelta=0,num_utenti=0,size_catalogo=countRows("brani.csv");
int size_publicP=0;
if(size_catalogo<0)
{ MessageBox(NULL,"Failed to open \"brani.csv\" ","ERROR MESSAGE",MB_ICONERROR|MB_ICONSTOP);
return -1;
}
Brano* catalogo=NULL;
catalogo=(Brano*)malloc(sizeof(Brano)*size_catalogo);
Brano** pCatalogo=NULL;
Playlist** publicPlaylists=NULL;
Utente* loggedUser=NULL;
pCatalogo=(Brano**)malloc(sizeof(Brano*)*size_catalogo);
Utente* utenti=NULL;
readCSV(catalogo);
}