-1

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);

}

Nik
  • 23
  • 1
  • 6
  • 4
    The title is screaming *undefined behavior!!!* This code is incomplete anyways. And poorly formatted. – Eugene Sh. Jun 07 '18 at 21:25
  • Yeah I know the code is incomplete but the other parts of the code aren't executed since it stops on readCSV, I also know there's some undefined behaviour but Idk what's causing it and how to fix it – Nik Jun 07 '18 at 21:27
  • `for (i=0;(!feof(file));i++)` see "Why `while(!feof(fp))` is always wrong". https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Christian Gibbons Jun 07 '18 at 21:27
  • Seriously? And how are we supposed to know what `MAX` is? – Eugene Sh. Jun 07 '18 at 21:27
  • @ChristianGibbons Incorrectly stating that the use of `while ( !feof() )` is wrong is almost as common as posting questions with code that incorrectly uses `while ( !feof() )`. In this case, the `if ( fgets( ... ) != NULL )` results in properly working code. – Andrew Henle Jun 07 '18 at 21:39
  • oh you're right on MAX now I fixed it, and even changing replacing the call to countRows with 53 which is the number it is supposed to return (and returns) and replacing the while(!feof(fp)) with 53 I still get the same error 255 – Nik Jun 07 '18 at 21:41

1 Answers1

1

Your getstring() is wrong. It does not account for the terminating NUL character.

  char* getString(char* line)
 {
   printf("%s",line);
   char* a=NULL;
   a=(char*)malloc(sizeof(char)*strlen(line));
   strcpy(a,line);
   return a;
 }

should be

 char *getString(char *line)
 {
     printf("%s",line);
     char* a=malloc(1 + strlen(line));
     strcpy(a,line);
     return a;
 }

Note that 1 + strlen() to account for the terminating NUL character.

Note also that sizeof(char) is always one by definition, and that you don't have to cast a void * in C.

Also, see strdup().

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Thank you so much for your answer! I can't believe I didn't notice that before, I think you saved my college life! – Nik Jun 07 '18 at 21:51