Hello Daniweb! :)

I am brand new to this site, and wish I didn't need the help but I do! So let's give it a go shall we?

I have a program I have been working on for about two weeks now, and I feel like it should not be taking me this long. So here I am, in need of the almighty Daniweb. I have looked at previous threads like requested, but none of them really seemed to speak to my cause, or answer my question. I am using file descriptors, and most of the other threads were not. Hopefully someone will be able to assist me! :-/

The overview:

I have three methods that I wrote, readBinary(), writeBinary(), and Update(). The program compiles just fine, but it doesn't do it's intended job... Which is...

Programs purpose: When the program starts, I want it to search for "players.bin" and if it doesn't exist, to create it! If it does find it, I want it to print out the records in that file in CSV format. I think I have done this properly, but feel free to tell me differently.

The records in the file would be something like "200 jones jim 23 34 56"

200 being the userid, jones being the last name, jim being the first name, 23 34 56 being wins, losses, and ties.

So that takes care of THAT. However, here is where I am stuck..

I have never used opcodes. But am doing my best in my attempts to do so. I have sort of an "update file" thats an .ascii. In theory, it's supposed to have instructions and records about those instructions to add to the .bin file.

Here is how the opcodes work:

1: Add a new player. The integer 1 is followed by player's info like I had previously stated "userid last first wins ties losses". (My writeBinary method should take care of this).

2: Update the scores for an existing record. The integer 2 followed by the same info above. An example would be 2 200 2 1 3 for add 2 to wins, 1 to losses, and 3 to ties for user 200. All player ids are unique so this makes it a bit easier. (My update method should take care of this)

3: Print the records and terminate the program. (My main function should take care of this, but I feel it may be a little lacking.)

This means that all the input strings from the ascii should look something like opcode userid last first wins ties losses. Or using real info --- 2 500 lastname firstname 44 55 66

Assumingly, there is 100 or less records to be dealt with.

Like I said, my code compiles but I cannot seem to get it to work? I do not want handouts/the answers; I would actually prefer the opposite. I want to learn how to do this so maybe some pseudo code comments can help me out? In advance, thank you SO much for your time!

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <string.h>

typedef struct {

   char first[10];
   char last[10];
   int userid, wins, losses, ties; 

} Player;

void readBinary() {
   int fd = 0;
   int num = -1;
   Player rec;
   fd = open("players.bin", O_RDONLY|O_CREAT, S_IRWXU);
   if (fd > 0) {
   
   while (num !=0) {
   num = read(fd, &rec, sizeof(rec)); 
   }  
      }
   if (num == 0){
   printf("End of record file\n");
   }
   else {
        printf("%d, %s, %s, %d, %d, %d\n", rec.userid, rec.last, rec.first, rec.wins, rec.losses, rec.ties);
        }
 
printf("\n");
close(fd);

}

   void writeBinary(){
   int fd = 0;
   int num;
   fd = open("players.bin", O_RDWR|O_APPEND|O_CREAT, S_IRWXU);
   Player rec; 
   if (fd > 0){
 
   for(int x=0; x<=100; x++){
   scanf("%d %s %s %d %d %d", &rec.userid, &rec.last, &rec.first, &rec.wins, &rec.losses, &rec.ties);
   }
}
   else { 
   printf("Error opening file\n"); 
}
close(fd);
} 



void update() {
int fd = 0;
Player rec;
Player temp;
int offset;
int num = -1;

scanf("%d %d %d %d", temp.userid, &temp.wins, &temp.losses, &temp.ties);
fd = open("players.bin", O_RDWR);
if (fd > 0) {
num = read(fd, &rec, sizeof(rec));
}
else  
printf("Error while trying to open file!\n");

offset = sizeof(rec);
fd = open("players.bin", O_RDWR);
if (fd > 0){
lseek(fd, offset, SEEK_SET);
num = read(fd, &rec, sizeof(rec));

rec.wins = rec.wins + temp.wins;
rec.losses = rec.losses + temp.losses;
rec.ties = rec.ties + temp.ties;
lseek(fd, 0, SEEK_SET);
lseek(fd, offset, SEEK_CUR);
num = write(fd, &rec, sizeof(rec));
}
else
printf("Error while opening file!\n");
close(fd);
}

int main(){
int opcode = 0;
readBinary();
while(opcode != 3) {
printf("Please enter the operation to be performed:\n");
scanf("%d", &opcode);
if(opcode == 1)
writeBinary();
if(opcode == 2)
update();
}
}

I have three methods that I wrote, readBinary(), writeBinary(), and Update(). The program compiles just fine, but it doesn't do it's intended job...

...

So that takes care of THAT. However, here is where I am stuck..

I have never used opcodes. But am doing my best in my attempts to do so. I have sort of an "update file" thats an .ascii. In theory, it's supposed to have instructions and records about those instructions to add to the .bin file.

...

Like I said, my code compiles but I cannot seem to get it to work? I do not want handouts/the answers; I would actually prefer the opposite. I want to learn how to do this so maybe some pseudo code comments can help me out? In advance, thank you SO much for your time!

After all that, you might want to tell us what's wrong, not just say it doesn't work. How do we know what to look for?

Also, you might want to format your code better so the rest of us can follow it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.