Skip to content

Instantly share code, notes, and snippets.

@binura-g
Created August 15, 2017 07:28
Show Gist options
  • Save binura-g/065ab122958a71211ab77161a46abf2a to your computer and use it in GitHub Desktop.
Save binura-g/065ab122958a71211ab77161a46abf2a to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <unistd.h>
#include <string.h>
struct record {
int userid;
char username[6];
};
char *usernames[] = {"userA", "userB", "userC", "userD"};
int main(int argc, char *argv[])
{
int i, outfile;
struct record eachrec;
if ((outfile == open("recordfile", O_WRONLY | O_CREAT, 0664))<0) {
perror("recordfile");
exit(1);
}
for (i = 3; i>=0; i--) {
eachrec.userid = i;
strcpy(eachrec.username, usernames[i]);
lseek(outfile, (long) i * sizeof(struct record), SEEK_SET);
write(outfile, &eachrec, sizeof(struct record));
}
close(outfile);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment