rendered paste body#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 5
struct Foititis{
char am[10];
char epi[20];
char ono[20];
float mo;
};
struct IFoititis{
char am[10];
int dief;
};
void selection_sort(struct IFoititis * , int);
main()
{
struct Foititis f;
struct IFoititis *foi , temp;
FILE *dataf;
FILE *indexf;
int pl , m=0 , i , j , tm;
int syn;
dataf = fopen("Foititis.dat" , "rb+");
if (!dataf) {
dataf = fopen("Foititis.dat" , "wb");
indexf = fopen("Foititis.ndx" , "wb");
fclose(indexf);
pl = 0;
}
else
{
indexf = fopen("Foititis.ndx" , "rb");
fseek(indexf , 0 , SEEK_END);
pl = ftell(indexf) / sizeof(struct IFoititis);
fclose(indexf);
fseek(dataf , 0 , SEEK_END);
}
m = pl;
foi = malloc((pl+N) * sizeof(struct IFoititis));
printf("\n\nEISAGOGI EGGRAFON\n\n");
do{
printf("\nEisagogi Aritmou Mitroou :");
scanf("%s", &f.am);
printf("\nEisagogi Epithetou :");
scanf("%s", &f.epi);
printf("\nEisagogi Onomatos :");
scanf("%s", &f.ono);
printf("\nEisagogi Mesou Orou :");
scanf("%f", &f.mo);
fwrite(&f , sizeof(struct Foititis) , 1 , dataf);
strcpy(foi[m].am , f.am);
foi[m].dief = m;
m++;
printf("\n\n Thelete nea eisagogi (1=NAI , 0=OXI) : ");
scanf("%d" , &syn);
} while ((syn == 1) && (m < pl+N));
fclose(dataf);
indexf = fopen("Foititis.ndx" , "rb+");
fseek(indexf , 0 , SEEK_END);
for (i=pl; i<m; i++)
fwrite(&foi[i] , sizeof(struct IFoititis) , 1 , indexf);
fclose(indexf);
indexf = fopen("Foititis.ndx" , "rb");
fseek(indexf , 0 ,SEEK_END);
pl = ftell(indexf) / sizeof(struct IFoititis);
fseek(indexf , 0 , SEEK_SET);
fread(&foi[0] , sizeof(struct IFoititis) , pl , indexf);
fclose(indexf);
selection_sort(foi , pl);
indexf = fopen("Foititis.ndx" , "wb");
fwrite(&foi[0] , sizeof(struct IFoititis) , pl , indexf);
fclose(indexf);
free(foi);
}
void selection_sort(struct IFoititis x[] , int D)
{
int i , j , t;
struct IFoititis temp;
for(i=0; i<D-1; i++) {
t = i;
for(j=i-1; j<D-1; j++)
if ( strcmp(x[t].am , x[j].am)>0)
t = j;
temp = x[i];
x[i] = x[t];
x[t] = temp;
}
}