Δημοσιεύτηκε: 25 Απρ 2013, 19:42
- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX 3
struct student {
char onoma[20];
char eponimo[25];
int am;
int year;
};
int main(int argc, char *argv[]) {
struct student *ptr,*step;
ptr=(struct student*)malloc(100*sizeof(struct student));
int fd,i,j;
char am[20],year[20];
struct student studs[MAX];
fd = open("catalog.txt", O_RDWR | O_CREAT , 0777);
if(fd<0){
perror("fd error");
}
for(i = 0; i < MAX; i++)
{
printf("Enter student data:\n");
printf("onoma: ");
scanf("%s",(ptr[i]).onoma);
printf("Last name: ");
scanf("%s",(ptr[i]).eponimo);
printf("Code: ");
scanf("%d", &(ptr[i]).am);
printf("ilikia ");
scanf("%d",&(ptr[i]).year);
}
for(i = 0; i < MAX; i++) {
printf("%s\t",(ptr[i]).eponimo);
printf("%s\t",(ptr[i]).onoma);
printf("%d\t", (ptr[i]).am);
printf("%d\t",(ptr[i]).year);
printf("\n");
}
for(i=0; i<MAX; i++) {
for(j=0; j<MAX; j++) {
if(strcmp(ptr[i].eponimo, ptr[j].eponimo) < 0){
step = ( struct student*)malloc(sizeof(struct student));
strcpy(step[0].eponimo,ptr[j].eponimo);
strcpy(step[0].onoma,ptr[j].onoma);
step[0].am = ptr[j].am;
step[0].year = ptr[j].year;
strcpy(ptr[j].eponimo, ptr[i].eponimo);
strcpy(ptr[j].onoma, ptr[i].onoma);
ptr[j].am = ptr[i].am;
ptr[j].year = ptr[i].year;
strcpy(ptr[i].eponimo, step[0].eponimo);
strcpy(ptr[i].onoma, step[0].onoma);
ptr[i].am = step[0].am;
ptr[i].year = step[0].year;
free(step);
}
}
}
printf("with taxinomisi\n");
for(i = 0; i < MAX; i++) {
printf("%s\t",(ptr[i]).eponimo);
printf("%s\t",(ptr[i]).onoma);
printf("%d\t", (ptr[i]).am);
printf("%d\t",(ptr[i]).year);
printf("\n");
}
for(i=0; i<MAX; i++) {
write(fd, ptr[i].eponimo,sizeof(ptr[i].eponimo)*sizeof(char));
write(fd, "\t", sizeof(char));
write(fd, ptr[i].onoma, sizeof(ptr[i].onoma)*sizeof(char));
write(fd, "\t", sizeof(char));
sprintf(am, "%d", ptr[i].am);
printf("%s", am);
write(fd, am, sizeof(am)*sizeof(char));
write(fd, "\t", sizeof(char));
sprintf(year, "%d", ptr[i].year);
write(fd, year, sizeof(year)*sizeof(char));
write(fd, "\n", sizeof(char));
}
close(fd);
free(ptr);
return 0;
}