Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include "stdio.

h"
struct date
{
int dd, mm, yy ;
};
struct emp
{
char name[10] ;
struct date jd ;
float salary ;
} ;
main( )
{
FILE *fp ;
struct emp e1, e2 ;
int i, j ,n = 10 ;
long pos ;
int fcmp_dates ( struct date *d1, struct date *d2 ) ;
clrscr( ) ;
fp = fopen ( "e:\\cd\\tcc\\emp.dat", "rb+" ) ;
if ( fp == NULL )
{
printf ( "Unable to open" ) ;
getch();
exit( ) ;
}
for ( i = 0 ; i < 4 ; i++,n-- )
{
fread ( &e1, sizeof(e1), 1, fp ) ;
for ( j = 1 ; j < n ; j++ )
{
fseek ( fp, 0L, SEEK_CUR ) ;
fread ( &e2, sizeof(e2), 1, fp ) ;
if( fcmp_dates ( &e1.jd, &e2.jd ) > 0 )
{
fseek ( fp, -2L * sizeof( struct emp ), SEEK_CUR
) ;
fwrite ( &e2, sizeof ( e2 ), 1, fp ) ;
fwrite ( &e1, sizeof ( e1 ), 1, fp ) ;
}
else
e1 = e2 ;
}
rewind ( fp ) ;
}
rewind ( fp ) ;
printf ( "\nThe Sorted List is : \n" ) ;
while ( fread ( &e1, sizeof( e1 ), 1, fp ) )
printf ( "\n%s\t %d/%d/%d \t%7.2f", e1.name, e1.jd.dd, e1.jd.mm,
e1.jd.yy, e1.salary ) ;
fclose ( fp ) ;

}
fcmp_dates ( struct date *d1, struct date *d2 )
{
int x, y ;
x = ( d1->yy - 1980 ) * 512 + d1->mm * 32 + d1->dd ;
y = ( d2->yy - 1980 ) * 512 + d2->mm * 32 + d2->dd ;
return ( x - y ) ;
}

You might also like