I am trying to write a program that will read the existing records of a file and then index them in another file. The records are stored in the file named "players.bin" which is CSV format each record contains (username,lastname,firstname,numwins,numlosses,numties and i want to index them in a new file named "players.idx". However the players.idx file will only contain a sequence of pairs of , where username is player's user name and seq# is the sequence number of the player's record stored in in the player file.
Here is what I have come up with so far:
fd = open("players.bin", O_WRONLY | O_CREAT |
O_APPEND, S_IRWXU);
if (fd > 0) {
//Read the contents of the file (if any)
//and then print out each record until the end of file
while (num = read(fd, &plyr, sizeof (plyr)) != 0) {
printf("%s, %s, %s, %d, %d, %d\n\n", plyr.user_name,
plyr.last_name, plyr.first_name, plyr.num_wins,
plyr.num_losses, plyr.num_ties);
}
close(fd);
}
fd2 = open("players.idx", O_WRONLY | O_CREAT |
O_APPEND, S_IRWXU);
if (fd > 0) {
while (num = read(fd, &plyr, sizeof (plyr)) != 0) {
num = write(NOT SURE WHAT TO PUT HERE);
record_count++; //I am going to use this to keep track of seq numbers
}
close(fd2);
}
I am just really confused on how to go about this...Thanks