Ok i decided to start from scratch for my CD database.
I've created an array structure, i can enter the fields, but for some reason it dont display to screen neone know why please???
sorted out the records thing.... but now i cant display and when i press the sort option it dont sort,, i dont get no errors when i compil tohugh.. please neone help me out here...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Idea here is to enter an array of structures
// write them to disk and read them back
// simple structure
struct tuple {
char artist[50];
char album[50];
char label[50];
int year[4];
char type[30];
};
int filewrite() {
// file pointer
FILE * mydata;
// structures
struct tuple myDB[20];
struct tuple otherDB[20];
// variable needed
int i;
// go through each record
// write each to disk using formatted output
mydata = fopen("mymusic.txt","w");
i=1;
while(i){
printf("Enter the artist: \n");
scanf("%s",myDB[i].artist);
printf("Enter the album: \n");
scanf("%s",myDB[i].album);
printf("Enter label name: \n");
scanf("%s",myDB[i].label);
printf("Enter year: \n");
scanf("%d",&myDB[i].year);
printf("Enter the type of music: \n");
scanf("%s",&myDB[i].type);
fprintf(mydata, "%s %s %s %d %s\n",myDB[i].artist, myDB[i].album, myDB[i].label,myDB[i].year,myDB[i].type);
printf("\n\n press 1 to continue,0 to stop");
scanf("%d",&i);
}
fclose(mydata);
{
// read the data
printf("\n\n\n\n\n\n\n\nreading data...\n");
mydata = fopen("mymusic.txt","r");
while(i){
fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, &otherDB[i].year, otherDB[i].type);
printf("\n\n\nRecord is...\n\n\n");
printf("%s\n",otherDB[i].artist);
printf("%s\n",otherDB[i].album);
printf("%s\n",otherDB[i].label);
printf("%d\n",otherDB[i].year);
printf("%s\n",otherDB[i].type);
}
fclose(mydata);
}
}
void main()
{
int c;
while(c!=5)
{
printf("GIVE CHOICE--\n");
printf(" 1 TO ENTER CD INFO.\n");
printf(" 2 TO SEE STUDENT.TXT FILE\n");
printf(" 3 TO SORT CDs BY YEAR\n");
printf(" 4 TO SEARCH USING ALBUM NAME\n");
printf(" 5 TO EXIT\n\n--");
scanf("%d",&c);
switch(c)
{
case 1:
filewrite();
break;
case 5:
break;
case 3:
filesort();
break;
default:
break;
}
}
}
int bubble(int x[],int n)
{
int hold,j,pass,i,switched = 1;
for(pass = 0; pass < n-1 && switched == 1;pass++)
{
switched=0;
for (j=0;j<n-pass-1;j++)
if (x[j]>x[j+1])
{
switched=1;
hold = x[j];
x[j] = x[j+1];
x[j+1]=hold;
}
}
return(0);
}
int filesort()
{
char artist[50];
char album[50];
char label[50];
int year[4],x[100],n,i,j;
char type[30];
FILE *fp,*fm;
FILE * mydata;
struct tuple myDB[20];
struct tuple otherDB[20];
fp = fopen("mymusic.txt","r");
fm = fopen("year.txt","w");
i=0;
while(! feof(fp))
{
fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, otherDB[i].year, otherDB[i].type);
x[i]= year[i];
i=i+1;
}
n=i;
bubble(x,n);
for(i=0;i<n;i++)
{
printf(" %d\t",x[i]);
}
for(i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(x[i]==year[j])
{
fprintf(mydata, "%s %s %s %d %s\n",myDB[j].artist, myDB[j].album, myDB[j].label,&myDB[j].year,myDB[j].type);
}
}
}
fclose(fm);
fclose(fp);
printf("\n\n\nPRESS ANY KEY");
getch();
}