Hey guys,

I am having a problem. When loadPref is executed, it opens, reads, and saves properly. But it crashes shortly after when the system call system("cls") happens. I do not understand why, any help? I removed most of the code that is not involved.

Tests / Outcomes
Removing loadPref before system("cls") / Runs
Placing system("cls") before while loop / Crash
Placing system("cls") after if statements / Crash
changing command from cls to dir / crash
Removing system("cls") / Runs
Removing printf and getchar / Crash

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


/*The function passes by reference. It opens a file, reads in the length of a certain folder name. Then reads in the folder name. Preforms the same thing for the next folder name. Finally, it builds a dynamic string array of no go areas which gathers folder names in the same way as before. I tried test after the function call and it presents the loaded data correctly.*/

void loadPref(char ** root, char **unsort, char *** noGo, int * noGoCount){
   int skip, k;
   FILE * data;
   
   data = fopen("CSData.bin", "rb+");

   if(data == NULL) {
      data = fopen("CSData.bin", "wb+");
      *root = malloc(sizeof(char)*strlen("\\Music"));
      strcpy(*root, "\\Music");
      k=strlen(*root);
      fwrite(&k, sizeof(int), 1, data);
      fwrite(*root, sizeof(char), strlen(*root), data);

      
      *unsort = malloc(sizeof(char)*strlen("\\!Unsort"));
      strcpy(*unsort, "\\!Unsort");
      k=strlen(*unsort);
      fwrite(&k, sizeof(int), 1, data);
      fwrite(*unsort, sizeof(char), strlen(*unsort), data);
      fwrite(0, sizeof(int), 1, data);
      *noGoCount = 0;
      *noGo = NULL;
      printf ("No previous preferences file found.\nPreferences are set to default.\n\n"); 
   }

   else {
      /*Have not tested this section yet 

      fread(&skip, sizeof(int), 1, data);
      *root = malloc(sizeof(char)*skip);
      fread(*root, sizeof(char), skip, data);

      
      fread(&skip, sizeof(int), 1, data);
      fread(*unsort, sizeof(char), skip, data);
      *unsort = malloc(sizeof(char)*skip);

      
      fread(&noGoCount, sizeof(int), 1, data);
      if(noGoCount >0){
         *noGo = malloc(sizeof(char *)*(*noGoCount));
         for (k=0; k<*noGoCount; k++){
            fread(&skip, sizeof(int), 1, data);
            *noGo[k] = malloc(sizeof(char)*skip);
            fread(*noGo[k], sizeof(char), skip, data);
         }
      }
      else {
         *noGo = NULL;               
         printf ("Preferences file loaded...\n\n");
      }
       */
   }
   fclose(data);
   data= NULL;
}

   
int main() {
   tag songinfo;
   char ** noGo, *root, *unsort;   
   int menu, NGC;
   FILE *song;
   song = NULL;
   

   loadPref(&root, &unsort, &noGo, &NGC);

   
   printf ("Press Enter To Continue");
   getchar();
  
   while (menu != 4){
      /*Crashes here*/
      system("cls");        
      printf("#-------------------------#\n");
      printf("|           Menu              |\n");
      printf("|                                 |\n");
      printf("|     1. Quick Sort        |\n");
      printf("|     2. Full Sort           |\n");       
      printf("|     3. Instructions     |\n");
      printf("|     4. About               |\n");
      printf("|     5. Exit                  |\n");
      printf("|                                 |\n");
      printf("#-------------------------#\n\n");            
      printf("Enter a number from 1-4: ");
      scanf("%d", &menu);
      getchar(); 
      printf("\n\n");
      
      if (menu == 1){
           /*Blarg*/
      }
           
      else if (menu == 2){
           /*Blarg*/
      }     

      else if (menu == 3){
           /*Blarg*/                  
      }
               
      else if (menu == 4){
           /*Blarg*/
      }
      
   }
   return 0;
}

Do not worry about the qualifier for the while loop. I have yet to update each if statement.

I found the culprit line. After immense commenting, the line 25...

strcpy(*unsort, "\\!Unsort"); / Crash
strcpy(*unsort, "\\Unsort"); / Runs

...passes undetected in the function but fails when the system calls. I use "!" because when creating files I want the \!Unsort file to be before the folder \A \B \C ... \Z . the line before will not cause the program to crash but this specific line will crash the system call. Any ideas?

*unsort = malloc(sizeof(char)*strlen("\\!Unsort"));
strcpy(*unsort, "\\!Unsort");

Yeah, you need to malloc (strlen(foo) + 1 ) bytes to copy a string foo.

The fact that it works with certain strings (and it's still wrong) just makes you lucky.

*unsort = malloc(sizeof(char)*strlen("\\!Unsort"));
strcpy(*unsort, "\\!Unsort");

Yeah, you need to malloc (strlen(foo) + 1 ) bytes to copy a string foo.

The fact that it works with certain strings (and it's still wrong) just makes you lucky.

Hey it works! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.