Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting records in a file by specified field(s)

Status
Not open for further replies.

swaroop

Programmer
Feb 4, 2001
100
US
Please any one help me in sorting records in a given file with a specified field.

Example:

LASTNAME FIRSTNAME ID JOINDATE SALARY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kunduru Swaroop 9234 10/10/2000 4000.00
Barrenkabavi Vishnu 2945 01/11/2000 7000.00
Junkey Naveen 2234 12/09/2000 1000.00
Chinnala Rajesh 6134 03/01/2001 5000.00
Kalyan Pavan 1234 12/27/2000 2000.00

The above records are in file Data.dat I wanted to sort by ID. Could any one help in solving this problem.

Thanx in advance.

Swaroop.
 
You need a struct:
struct dataStr
{
string LASTNAME;
string FIRSTNAME
int ID;
string JOINDATE;
float SALARY;
};
Load file into an array of dataStr's and sort it. After it write them in correct order in your file.
John Fill
1c.bmp


ivfmd@mail.md
 
Greetingx!

Also you may read data to early presented structure using fscanf function, like follow:

FILE *file;
char LASTNAME[20], FIRSTNAME[20],JOINDATE[10];
int ID;
float SALARY;
file = fopen("yourfile.ext","r");
fscanf(file,"%s %s %i %s %f",LASTNAME,FIRSTNAME,&ID,JOINDATE,&SALARY);
//here writing read data to dataStr structure;
fclose(file);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top