[prev] 52 [next]

Structured Data (cont)

Example of write()ing records vs printf()ing records

typedef struct _student {
    int id; char name[99]; float wam;
} Student;
int infd, outfd; // file descriptors
FILE *inf, *outf; // file pointers

Student stu;  ... set values in stu.id, etc ...

write(outfd, &stu, sizeof(struct _student));
  vs
fprintf(outf, "%d:%s:%f\n",
             stu.id, stu.name, stu.wam);

read(infd, &stu, sizeof(Student));
  vs
fscanf(inf, "%d:%[^:]:%f\n", 
            &(stu.id), &(stu.name), &(stu.wam));