From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/03.2.c | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/03.2.c (limited to 'reference/C/CONTRIB/OR_USING_C/03.2.c') diff --git a/reference/C/CONTRIB/OR_USING_C/03.2.c b/reference/C/CONTRIB/OR_USING_C/03.2.c new file mode 100644 index 0000000..69d8904 --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/03.2.c @@ -0,0 +1,67 @@ +/* + * If you're on System V, change to . + */ +#include + +struct record { + int uid; + char login[9]; +}; + +char *logins[] = { "user1", "user2", "user3", + "user4", "user5" }; + +main() +{ + int i, fd; + struct record rec; + + /* + * Open the data file for writing. + */ + if ((fd = open("datafile", O_WRONLY | O_CREAT, 0644)) < 0) { + perror("datafile"); + exit(1); + } + + /* + * For each user, going backwards... + */ + for (i = 4; i >= 0; i--) { + /* + * Create the record. + */ + rec.uid = i; + strcpy(rec.login, logins[i]); + + /* + * Output the record. Notice we pass the + * address of the structure. + */ + putrec(fd, i, &rec); + } + + close(fd); + exit(0); +} + +/* + * putrec--write the record in the i'th position. + */ +putrec(fd, i, r) +int i, fd; +struct record *r; +{ + /* + * Seek to the i'th position from the beginning + * of the file. + */ + lseek(fd, (long) i * sizeof(struct record), L_SET); + + /* + * Write the record. We want to write one + * object the size of a record structure. + */ + write(fd, r, sizeof(struct record)); +} + -- cgit v1.2.3-54-g00ecf