Skip to content

Instantly share code, notes, and snippets.

@Schabernack
Created January 18, 2010 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schabernack/280251 to your computer and use it in GitHub Desktop.
Save Schabernack/280251 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
typedef struct
{
unsigned short telefonNummer;
char name[20];
char vorname[20];
char raum[5];
} telListe_typ;
void printlist(int fd){
telListe_typ buf;
lseek(fd, 0, SEEK_SET);
while ((read(fd,&buf, sizeof(telListe_typ))) == sizeof(telListe_typ )){
printf("Name: %s, %s (%s) TEL: %d \n",buf.name, buf.vorname, buf.raum, buf.telefonNummer);
}
}
int main(int argc, char *argv[]){
int datei = open ("tel.dat",O_RDONLY, S_IREAD);
int tmp = open("tmp.dat", O_CREAT | O_RDWR, S_IRWXU);
int size;
telListe_typ puffer;
//copy tel.dat in tmp.dat
while ((size = read(datei,&puffer, sizeof(telListe_typ))) == sizeof(telListe_typ)){
write(tmp, &puffer, size);
}
//Anzahl der Datensätze ermitteln
int datensaetze;
printf("Anzahl der Datensaetze: %d \n", datensaetze=(lseek(tmp, 0, SEEK_END)/sizeof(telListe_typ)));
//Nach telefonnr sortieren
int i;
int n;
telListe_typ puffer2 ;
for(i=datensaetze-1; i>0; i--){
lseek(tmp,0,SEEK_SET);
for(n=0; n<i; n++){
read(tmp, &puffer, sizeof(telListe_typ));
read(tmp, &puffer2, sizeof(telListe_typ));
if(puffer.telefonNummer > puffer2.telefonNummer){
lseek(tmp, -2*sizeof(telListe_typ),SEEK_CUR);
write(tmp, &puffer2, sizeof(telListe_typ));
write(tmp, &puffer, sizeof(telListe_typ));
}
lseek(tmp, -1*sizeof(telListe_typ), SEEK_CUR);
}
}
printlist(datei);
printf("\n\n\n\n");
printlist(tmp);
close(datei);
close(tmp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment