7 Replies - 4817 Views - Last Post: 21 April 2009 - 08:15 AM Rate Topic: -----

#1 preeengles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 09

C | Sorting records in a file

Posted 20 April 2009 - 05:22 AM

I want to arrange the records in a file in ascending order. Is there any predefined function for this? If there's none, can anyone give me anything (a tutorial link, snippet, sample code, or algorithm, etc.) that would be helpful in solving my problem?

Thanks in advance.

P.S. Sorry about the double post. Our connection's slow so I refreshed the page and the other post was created. Sorry :(

This post has been edited by preeengles: 20 April 2009 - 05:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C | Sorting records in a file

#2 junxuan  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 25-March 09

Re: C | Sorting records in a file

Posted 20 April 2009 - 05:54 AM

im not sure how u can modify the file directly, but u could try reading the values in the file, sort, delete the contents of the file and finally put the sorted data back in.
Was This Post Helpful? 0
  • +
  • -

#3 preeengles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 09

Re: C | Sorting records in a file

Posted 20 April 2009 - 06:08 AM

I've thought of the same thing but I think it would be too time consuming if I used that method. Maybe there's a predefined function for it or something...
Was This Post Helpful? 0
  • +
  • -

#4 junxuan  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 25-March 09

Re: C | Sorting records in a file

Posted 20 April 2009 - 06:27 AM

im not sure about c, but im quite sure c++ doesnt have one.

btw time consuming meaning the algorithm is time consuming or writing the code?

This post has been edited by junxuan: 20 April 2009 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C | Sorting records in a file

Posted 20 April 2009 - 06:33 AM

View Postpreeengles, on 20 Apr, 2009 - 05:08 AM, said:

I've thought of the same thing but I think it would be too time consuming if I used that method. Maybe there's a predefined function for it or something...


Could you detail exactly what it is you want to have happen.

At this point it isn't very clear how you want to sort the contents of a file without bringing it into memory.

I suspect we may not be communicating effectively.
Was This Post Helpful? 0
  • +
  • -

#6 preeengles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 09

Re: C | Sorting records in a file

Posted 20 April 2009 - 10:56 PM

View Postjanotte, on 20 Apr, 2009 - 05:33 AM, said:

Could you detail exactly what it is you want to have happen.

At this point it isn't very clear how you want to sort the contents of a file without bringing it into memory.

I suspect we may not be communicating effectively.


Well, yeah I guess...Maybe you should see my code...This is due today so please, please help!

void disp_all()
{
   record stud;
   int i, rec_num = 0;
   FILE *fp1;
/************************************************************/
   //this part here has something to do with counting the number of records in the file

   rewind(fp);
   clrscr();
	if((fp1=fopen("new.dat","wb")) == NULL)
	{
		printf("\n\nUnable to Open new.dat File - exiting\n\n");
		exit(1);
	}

	while(fread(&stud, sizeof(stud), 1, fp))
	{
		if(stud.snum > 0)
		{
			fwrite(&stud, sizeof(stud), 1, fp1);
			rec_num++;
		}
   }

   fclose(fp);
   fclose(fp1);
   if((remove("std.dat")) != 0)
   {
	  printf("\n\nError! Obsolete file: std.dat, was not deleted - exiting\n\n");
	  exit(1);
   }
   else
   {
	  rename("new.dat", "std.dat");
	  if((fp=fopen("std.dat","rb+")) == NULL)
	  {
			if((fp = fopen("std.dat", "wb+")) == NULL)
			{
				printf("\n\nUnable to Open std.dat File - exiting\n\n");
				getch();
			exit(1);
		 }
	  }
   }
/************************************************************/
   //this part displays the records
   rewind(fp);
   printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE  ");
   while(fread(&stud,sizeof(stud),1,fp))
   {
	  if(stud.snum > 0)
	  {
	 
		 printf("%lld\t", stud.snum);	//student number
		 printf("%-16s", stud.lname);
		 printf("%-16s", stud.fname);
		 printf("%-12s", stud.mi);
		 printf("%-12s", stud.bday);
		 printf("%-12s\n", stud.course);
	  }
   }
   printf("\n\nThere are currently %d student records\n\n", rec_num);
   printf("\n\nPress any key to go back to menu.");
   getch();
}


That part that displays the records? How should I edit it so that it will display the records in ascending order based on student number?
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C | Sorting records in a file

Posted 21 April 2009 - 06:40 AM

So I take it you have a struct somewhere called 'stud'?

The easiest way would be to create an array of structs then sort the array then output the sorted array.

Have a read here:
http://www.cplusplus...ial/structures/

Make any sense?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: C | Sorting records in a file

Posted 21 April 2009 - 08:15 AM

View Postjanotte, on 21 Apr, 2009 - 07:40 AM, said:

So I take it you have a struct somewhere called 'stud'?


Well, a typedef called record, that references a struct, at least.

You have to read the data into an array to sort it. You should reasonably have a few functions that look like this:
void displayRecords(record studList[], int size) {
	int i;
	
	printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE  ");
	for (i=0; i<size; i++) {
		printf("%lld\t", studList[i].snum);
		printf("%-16s", studList[i].lname);
		printf("%-16s", studList[i].fname);
		printf("%-12s", studList[i].mi);
		printf("%-12s", studList[i].bday);
		printf("%-12s\n", studList[i].course);
	}
	printf("\n\nThere are currently %d student records\n\n", size);
	printf("\n\nPress any key to go back to menu.");
}

int loadRecords(char *fileName, record studList[]) { 
	record stud;
	int readCount;
	FILE *fp;
	
	if((fp=fopen(fileName,"rb")) == NULL) { return 0; }

	readCount = 0;
	while(fread(&stud,sizeof(stud),1,fp)) {
		if(stud.snum > 0) { studList[readCount++] = stud; }
	}
	return readCount;
}

void sortRecords(record studList[], int size) { /* your code here */ }



I've given too much away already. Learn how to sort arrays.

Good luck.

This post has been edited by baavgai: 21 April 2009 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1