Code:
typedef struct node Node;
struct node {
cd_t CD;
Node *next;
};
typedef struct {
Node *head; // pointer to the head of the list
int num_CDs;
} cd_collection_t;
void ReadCollectionFromBinFile (cd_collection_t *collection)
{
FILE *bfp;
char filename[15], choice='Y';
Node *node;
(*collection).head = (Node*) malloc(sizeof(Node));
(*collection).head->next = NULL ;
node = (*collection).head;
printf("\nEnter the name of the file to be read: ");
scanf("%s", filename);
if((*collection).num_CDs!=0) {
printf("\nData in memory will be overwritten, OK? (Y or N): ");
scanf(" %c", &choice);
}
else node = (Node*) malloc(sizeof(Node));
if(choice=='Y' || choice=='y')
{
while((bfp=fopen(filename, "rb"))==NULL)
{
printf("\nCould not open the file, enter the name again: ");
scanf("%s", filename);
}
while(node != NULL) {
fread(node, sizeof(Node), 1, bfp);
node->next = (Node*) malloc(sizeof(Node));
node = node->next;
node->next = (*collection).head;
(*collection).head = node;
}
printf("\nSuccess: Data has been written to memory.");
fclose(bfp);
}
printf("\nReturning to menu...");
}
void ReadCollectionFromTextFile (cd_collection_t *collection){
FILE *txtfp;
char filename[20];
char line[100], choice='Y';
int i=0;
Node *new_node = (Node*) malloc(sizeof(Node));
(*collection).head = (Node*) malloc(sizeof(Node));
(*collection).head->next = NULL;
(*collection).num_CDs=0;
if((*collection).num_CDs>0){
printf("There is data in memory. Continue will overwrite. OK? Y or N: ");
scanf(" %c", choice);
}
if(choice == 'Y') {
printf("\nEnter the name of the file to be read: ");
for(scanf("%s", filename); (txtfp=fopen(filename, "w")) == NULL; scanf("%s", filename))
printf("\nFile could not be opened, reenter file name: ");
fgets(line,100,txtfp);
if(feof(txtfp))
printf("\nFile is empty.");
else{
while(!feof(txtfp)&&(*collection).num_CDs<CSIZE){
printf("\nZOOB");
new_node->next = (*collection).head;
(*collection).head = new_node;
ReadCDFromTextFile (new_node,txtfp);
new_node->next = (Node*) malloc(sizeof(Node));
(*collection).num_CDs++;
}
}
}
fclose(txtfp);
}
//***************************************************************************
void ReadCDFromTextFile ( Node* cd, FILE *txtfp){
char line[100];
char delim[]=":";
char *token;
int i=0;
cd->CD.total_time_mins = cd->CD.total_time_secs=0;
fgets(line,100,txtfp);
if(feof(txtfp))
printf("\nFile has ended.");
else{
while(!feof(txtfp) && line[0]!='\n'){
if(line[0]=='\n')
fgets(line,100,txtfp);
strcpy(cd->CD.album,line);
printf("\n%s",cd->CD.album);
fgets(line,100,txtfp);
strcpy(cd->CD.publisher,line);
printf("\n%s",cd->CD.publisher);
fscanf(txtfp,"%s",cd->CD.identifier);
printf("\n%s",cd->CD.identifier);
fscanf(txtfp,"%d",cd->CD.num_tracks);
fgets(line,100,txtfp);
fgets(line,100,txtfp);
cd->CD.CDType=line[0];
if(cd->CD.CDType=='s'||cd->CD.CDType=='S'){
fgets(line,100,txtfp);
strcpy(cd->CD.track_info[0].artist,line);
fgets(line,100,txtfp);
while(line[0]!='\n'){
token = (char*)strtok(line,delim);
strcpy(cd->CD.track_info[i].track_name,token);
printf("\n%s",cd->CD.track_info[i].track_name);
token = (char*)strtok(NULL,delim);
cd->CD.track_info[i].minutes=atoi(token);
printf("\nMin: %d",cd->CD.track_info[i].minutes);
token = (char*)strtok(NULL,delim);
cd->CD.track_info[i].seconds=atoi(token);
printf("\nSec:%d",cd->CD.track_info[i].seconds);
fgets(line,100,txtfp);
i++;
if(feof(txtfp))
break;
}
}
else if(cd->CD.CDType == 'm' || cd->CD.CDType == 'M' ){
fgets(line,100,txtfp);
while(line[0]!='\n'){
token=(char*)strtok(line,delim);
strcpy(cd->CD.track_info[i].artist,token);
printf("\n%s",cd->CD.track_info[i].artist);
token = (char*)strtok(NULL,delim);
strcpy(cd->CD.track_info[i].track_name,token);
printf("\n%s",cd->CD.track_info[i].track_name);
token = (char*)strtok(NULL,delim);
cd->CD.track_info[i].minutes=atoi(token);
printf("\nMin: %d",cd->CD.track_info[i].minutes);
token=(char*)strtok(NULL,delim);
cd->CD.track_info[i].seconds=atoi(token);
printf("\nSec: %d",cd->CD.track_info[i].seconds);
i++;
if(feof(txtfp))
break;
fgets(line,100,txtfp);
}
}
cd->CD.total_time_mins+=cd->CD.track_info[i].minutes;
cd->CD.total_time_secs+=cd->CD.track_info[i].seconds;
if(cd->CD.total_time_secs>=60) {
cd->CD.total_time_secs-=60;
cd->CD.total_time_mins+=1;
}
}
}}