'ello

whenever i compile i keep getting an error saying identifier or declarator expected....for some reason i cant correct it neone know whats wrong???

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




void filewrite(void);
void fileprint(struct tuple *myDB);

// 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];
	char year[4];
	char type[30];
};

/*********************************************************************************************************/	
void filewrite(void) {
	
	// file pointer
	FILE *mydata;
	
	// structures
	struct tuple myDB[3];
	struct tuple otherDB[3];
		
	// variable needed
	int i;
	
	i=1;
	mydata = fopen("mymusic.dat","wb");
	
	while(i) {	
		
		printf("Enter the artist name: \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);
	}
	// don't forget to close the file
	fclose(mydata);
	
	
}
/********************************************************************************************/
void fileprint(struct tuple *myDB ){
FILE *mydata;

int i;

printf("reading data...\n");
	
	mydata = fopen("mymusic.dat","rb");
	{
		fscanf(mydata,"%s     %s     %s     %d     %s",myDB[i].artist, myDB[i].album, myDB[i].label, &myDB[i].year, myDB[i].type);
		
		printf("Record is...\n");
		printf("%s\n",myDB[i].artist);
		printf("%s\n",myDB[i].album);
		printf("%s\n",myDB[i].label);
		printf("%d\n",myDB[i].year);
		printf("%s\n",myDB[i].type);
		
	}
	// close it again
	fclose(mydata);
	}
}
	}
/*****************************************************************************************/
int main(void){
		int c;

	while(c!=6)
   		{  
     
     printf("GIVE CHOICE--\n");
     printf("   1 TO CD Information\n");
     printf("   2 TO Print Data On Screen\n");
     printf("   6 TO EXIT\n\n--");
     scanf("%d",&c);
     
     switch(c)
	 {
	 case 1:
		  filewrite();
		  break;
	 case 2:
		  fileprint();
		  break;
	 case 6:
		  break;
	 default:
		  break;
	 }
    }

  }

I also forgot to ask,,
i've been reading books on C they all show me how to sort and search arays that are stored in memory.. But for some reason im finding it difficult to understand how i would sort and search for array of structures, that are stored on a txt file... neone know how to explain ondoing this...

apreciate any help..

whenever i compile i keep getting an error saying identifier or declarator expected....for some reason i cant correct it neone know whats wrong???

Yes -- count the braces in function fileprint(). There are too many closing braces.


The easiest way to sort files is to read all the records in memory, sort them in memory, the rewrite the file in sorted order. When the file is too large to read into memory all at one time then the sort algorithm becomes very complicated. You have to sort the files in small sections, then do merge-sorts on each of the sorted sections.

Once the file has been sorted, its not all that difficult to search for specific record. If the records are fixed-length, then you can use binary sort algorithm. You can use binary sort on variable-length records too, but you will have to first construct an array of offsets to the beginning of each record.

how do i read all the records from the file into memory???

use normal file read functions. If you have never done that before you need to read some tutorials about file i/o. In c++ it might go something like below. After that, you will have an array (vector) of structures.

#include <fstream>
#include <vector>
using namespace std;

struct person
{
   char name[80];
   int age;  
   char address[126];
   char city[80];
   char state[3];
}

int main()
{
  vector<person> people;
   person record;
   ifstream in ("person.dat");
   if( in.is_open() )
   {
      while( in.read((char *)&record,sizeof(record)))
      {
               people.push_back(record);
      } 
      in.close();
   }

}
This article has been dead for over six months. Start a new discussion instead.