Created
August 15, 2017 07:28
-
-
Save binura-g/065ab122958a71211ab77161a46abf2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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