dynamically allocating memory
hello, I have created a database for music cds:
Code:
#include<stdio.h>
#include<stdlib.h>
#define array
typedef struct cd_database
{
char artist[200];
char title[200];
int tracks;
int album;
float price;
}cd_t;
#ifndef array
cd_t *cds;
#else
cd_t cds[100];
#endif
char fname[100];
int changes = 0;
int count = 0;
#define FLUSH while((ch = getchar()) != '\n' && ch != EOF)
void quit_program()
{
puts("Thanks for using the database. Have a nice day...");
system("pause");
}
int yesno(char prompt[])
{
char answer;
for(;;)
{
printf("%s (Y/N)? ",prompt);
fflush(stdin);
scanf (" %c",&answer);
answer = toupper(answer);
if(answer == 'Y')
return 1;
if(answer == 'N')
return 0;
}
}
are_you_sure()
{
if(!changes)
return 1;
return yesno("Are you sure you want to discard all records?");
}
reset_all()
{
changes = 0;
fname[0] = '\0';
}
input()
{
printf("Title: ");
fflush(stdin);
scanf ("%[^\n]",cds[count].title);
printf("Artist: ");
fflush(stdin);
scanf ("%[^\n]",cds[count].artist);
printf("Tracks: ");
fflush(stdin);
scanf ("%d",&cds[count].tracks);
cds[count].album = yesno("Album? ");
printf("Price: ");
fflush(stdin);
scanf ("%f",&cds[count].price);
}
output(int i, FILE *fptr)
{
printf("Title: %s\n",cds[i].title);
printf("Artist: %s\n",cds[i].artist);
printf("Tracks: %d\n",cds[i].tracks);
if(cds[i].album == 1)
printf("Album\n");
else
printf("Single\n");
printf("Price: %.2f\n\n",cds[i].price);
}
int main(void)
{
atexit(quit_program);
for(;;)
menu();
return 0;
}
menu()
{
int choice;
int ch;
/*forever loop until user enters a valid choice*/
for(;;)
{
printf("Current file open: %s\n\n",fname);
printf("Welcome to CD database...\n\n");
printf("Enter your choice:\n"
" 1. File: Create a new list\n"
" 2. Open an existing list\n"
" 3. Save list\n"
" 4. Save the list with a new name\n"
" 5. Append list to another file\n"
" 6. Edit: Create a new record\n"
" 7. View: Display a specific record\n"
" 8. Display all records\n"
" 9. Exit\n");
scanf("%d",&choice);
FLUSH;
if(choice == 1)
{
file_new();
break;
}
else if(choice == 2)
{
file_open();
break;
}
else if(choice == 3)
{
file_save();
break;
}
/* else if(choice == 4)
{
file_saveas();
break;
}
else if(choice == 5)
{
file_append();
break;
}*/
else if(choice == 6)
{
new_record();
break;
}
/*else if(choice == 7)
{
specific_record();
break;
}*/
else if(choice == 8)
{
all_records();
break;
}
else if(choice == 9)
{
exit(0);
break;
}
}
}
//option 1:
file_new()
{
if(are_you_sure())
reset_all();
system("cls");
return;
}
//option 2:
file_open()
{
printf("Enter the name of the file you would like to open: ");
fflush(stdin);
fgets (fname,sizeof(fname),stdin);
fname[strlen(fname)-1] = '\0';
// strcat(fname,".txt");
return;
}
//option 3:
file_save()
{
FILE *fptr;
int i;
fptr = fopen(fname,"wb");
if (fptr == NULL)
{
perror("Could not write to file");
return;
}
for(i = 0; i < count; i++)
fwrite(&cds,sizeof(cd_t),1,fptr);
fclose(fptr);
return;
}
//option 6:
new_record()
{
int i = count;
printf("Enter your record\n");
#ifndef array
if((cds = realloc(cds, sizeof(cd_t) * (i+1))) == NULL)
{
perror("Could not locate enough memory");
return;
}
#endif
input();
count++;
return;
}
//option 8:
all_records()
{
FILE *fptr;
int i=0;
fptr = fopen(fname,"rb");
if(fptr == NULL)
{
perror("Could not open");
return;
}
printf("Records\n");
while(fread(&cds,sizeof(cd_t),1,fptr) == 1)
{
output(i,fptr);
i++;
}
fclose(fptr);
return;
}
When I am using malloc instead of arrays the code is not working properly after exit. I have tried alot but can't came up with a way, can someone please help me out. Thanks in advance :)