im trying to do a sort for the array structure records that i have entered...
so far i've tried to use a book to do the coding...but im lost.. can anyone tell me if im on the right track or where im going wrong!!!

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

void filewrite(void);
void fileprint(struct tuple *myDB);
void quicksort(struct tuple *myDB,FILE *fp, int left, int right);
// 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 *fp;
	// structures
	struct tuple myDB[3];
	struct tuple otherDB[3];
	// variable needed
	int i;
	fp = fopen("mymusic.txt","wb");
	while(i) {	
		printf("Enter the artist name: \n");
		printf("Enter the album: \n");
		printf("Enter label name: \n");
		printf("Enter year: \n");
		printf("Enter the type of music: \n");
		fprintf(fp, "%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");
	// don't forget to close the file

void quicksort(struct tuple *myDB,FILE *fp, int left, int right)

if((fp=fopen("mymusic.txt", "rb+")) ==NULL) {
	printf("Cannot Open File For Read/Write.\n");
	printf("Sorting disk file.\n");
	quick_disk(fp, cd);
	printf("List Sorted.\n");
	/*A QuickSort For File.*/
	void quick_disk(FILE *fp, int count)
	qs_disk(fp, 0, count-1);
	void qs_disk(FILE *fp, int left, int right)
		int i,j;
		char x[100];
		i=left; j=right;
	//	strcpy(x, myDB[i].year(fp,(int)(i+j)/2));/*get the middle zip*/
	do {
		while(strcmp(myDB[i].year(fp,i),x)<0 && i<right) i++;
		while(strcmp(myDB[i].year(fp,j),x)>0 && j>left) j--;
		if(i<=j) {
			swap_all_fields(fp, i, j);
			i++; j--;
	  } while(i<=j);
	  if(left<j) qs_disk(fp, left, (int) j);
	  if(i<right) qs_disk(fp, (int) i, right);
	 void swap_all_fields(FILE *fp, int i, int j)
	 	char a[sizeof(cd)], b[sizeof(cd)];
		/*first read in record i and j */
	fseek(fp, sizeof(cd)*i, SEEK_SET);
	fread(a, sizeof(cd), 1, fp);
	fseek(fp, sizeof(cd)*j, SEEK_SET);
	fread(b, sizeof(cd), 1, fp);
	/*then write them back in opposite slots*/
	fseek(fp, sizeof(cd)*j, SEEK_SET);
	fwrite(a, sizeof(cd), 1, fp);
	fseek(fp, sizeof(cd)*i, SEEK_SET);
	fwrite(b, sizeof(cd), 1, fp);

int main(void){
		int c;

     printf("GIVE CHOICE--\n");
     printf("   1 TO CD Information\n");
     printf("   2 TO Print Data On Screen\n");
     printf("   6 TO EXIT\n\n--");
	 case 1:
	 case 2:
	 case 6:


Also will it be possible for me to add a critical count, analysis purposes to this sorting??

if you want a binary file, when you are reading/writing it wrong. Notice how much easer this is than writing/reading in text mode?

// write a record
fwrite(&myDB[i], 1, sizeof(MyDB[i]), fp);

// read a record
fread(&myDB[i], 1, sizeof(MyDB[i]), fp);

Does your program even compile correctly? It looks like you have functions inside functions, and possibly undeclared variables -- like cd.

Suggestion, use qsort of stdlib.h (I think)...
you only need to write the compare function and specify the struct size.

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