Hi Everyone
Hope you are all well.

I've been tasked with an assignment at collage to create an employee management system, so far i've done well and managed to create a working program which i am happy with :) However there is an optional function that can be created that will sort records based on the desired field. I've been doing well up until now, i just can figure out the correct/best way to compare the information for sorting.

I understand techniques that can be used, for example setting up two counter controlled loops to go through arrays making comparisons. i'm just not sure how to implement this when reading structures from a text file.

my structure looks like this

struct Employee_Data {
	int Employee_Number;    
	char Title[10];
	char First_Name[30];
	char Surname_Name[30];
	char Middle_Name[30];
	char DOB[10];
	char Gender[1];
	float Salary;
};

and i read the data from the saved file using

void Read_Info(void)
{
	system("cls");
	FILE *EMS;
	struct Employee_Data Input;
	EMS = fopen("HREMS.dat","r");
	if(!EMS)
	{
		puts("No data");
		Pause();
		return;
	}

	while(fread(&Input,sizeof(Input),1,EMS))
	{
             //Display data
	}
	Pause();
	fclose(EMS);
}

Should i create an Array of structures and copy the Information using the void Read_Info(void) function into each struct?

Any help will be greatly appreciated.

Many Thanks

Should i create an Array of structures and copy the Information using the void Read_Info(void) function into each struct?

Yep. This is fine for school assignments unless there's a specific requirement to handle huge files. It's vastly simpler to store all of the records in memory and then resort[*] as necessary.


[*] Alternatively, you can select the kth sorted element rather than sorting the whole array if the pattern of queries on a single key field is sparse, or the key field changes more often than not.

Thanks for the clarifying Narue

I'm anticipating coming back with more questions after/during writing a function to do this :)

Ok I’m back!

I thought i could be cheeky and just make a few modifications to my Read_Info() function to read data into a structure array.

Here's what I am trying:

void Sort_Info(void)
{
	system("cls");
	int x=0;
	FILE *EMS;
	struct Employee_Data Input;
	struct Employee_Data Hold[x];
	EMS = fopen("HREMS.dat","rb");
	if(!EMS)
	{
		puts("No data");
		Pause();
		return;
	}
	while(fread(&Input,sizeof(Input),1,EMS))
	{
		//I first tried this:
		//fscanf(EMS,"%d %s %s %s %s %f",Hold[x].Employee_Number,Hold[x].Title,Hold[x].First_Name,Hold[x].Surname,Hold[x].DOB,Hold[x].Salary);
		
		
		//Then i tried this
		//Hold[x].Employee_Number = Input.Employee_Number;
		//strcpy(Hold[x].Title, Input.Title);
		//strcpy(Hold[x].First_Name, Input.First_Name);
		//strcpy(Hold[x].Surname, Input.Surname);
		//strcpy(Hold[x].DOB, Input.DOB);
		//Hold[x].Salary=Input.Salary;
		
		x++;
	}
		printf("%s\n%s\n",Hold[0].First_Name,Hold[0].Surname);	//To Check data
	Pause();
	fclose(EMS);
}

I've had more success with the "Then i tried this" part but i can only seem to strcpy() one item. If I try to do the whole thing my program crashes.

Could someone tell me if I’m close? And possibly give me a nudge in the right direction :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.