You are my last chance

This is a discussion on You are my last chance within the C Programming forums, part of the General Programming Boards category; Ok guys, I know I'm really annoying posting questions every day, but this forum is my last chance to finish ...

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72

    You are my last chance

    Ok guys, I know I'm really annoying posting questions every day, but this forum is my last chance to finish my university tasks. I have only 1 more day.

    I need your help with a binary file.

    The question is quite similar to one of my last posts.

    I need to read a binary file like this:

    cBoard#10
    Bruce the boss#12
    Darth Vader FTW#16
    Albert#17
    William Wallace#21
    Where the text before the # is the name of the player and the number after the # is the number of dice rolls that the player needed to win.

    As you can see, all of they are ordered from the one with less dice rolls to the one with most dice rolls. And there are only 5 players, the TOP 5 players of all time.

    After play the game, I have to compare the dice rolls from the winner with all that 5 players. If the winner needed less dice rolls than one of the other players, the program has to write the ranking again with the new order.


    I'm using this code to read one line like "Darth Vader FTW#16":

    Code:
    fread(strLinea, sizeof(char), 100, f);
    And this one to write it on the file:
    Code:
    strcpy(strAux,jugador[i].strNom);itoa(jugador[i].nTiradestotals,strTirades,2);
    strcat(strAux,"#");
    strcat(strAux, strTirades);
    fwrite(strAux, sizeof(char), 100, f);
    jugador[i].strNom is the name of the winner.
    jugador[i].nTiradestotals is the number of dice rolls needed to win.


    But I think I'm not doing it good, it doesn't work.
    And I don't know how to read-compare-upload.

    If you, guys, can help me, I will be eternally grateful.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    itoa - C++ Reference
    So I'm wondering why you're converting an int in base 2.

    Is strTirades large enough to hold 33/65 characters (depending on the size of your ints)?

    > fread(strLinea, sizeof(char), 100, f);
    > fwrite(strAux, sizeof(char), 100, f);
    These two things are fine in themselves, they match one another.

    However, having done the fread() into strAux, you need to be mindful of this.
    strcpy(strAux,jugador[i].strNom);
    strcat(strAux,"#");
    strcat(strAux, strTirades);

    To get the name, you have to find the # character, and take all the characters before it.
    To get the number, you take everything after the # up to the next \0 (or 100 characters), then pass it to say atoi(), using the same base you used with itoa()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Quote Originally Posted by Salem View Post
    itoa - C++ Reference
    So I'm wondering why you're converting an int in base 2.
    My fault, it's not base 2 haha


    Is strTirades large enough to hold 33/65 characters (depending on the size of your ints)?
    strTirades[100] is enough, I think.

    > fread(strLinea, sizeof(char), 100, f);
    > fwrite(strAux, sizeof(char), 100, f);
    These two things are fine in themselves, they match one another.

    However, having done the fread() into strAux, you need to be mindful of this.
    strcpy(strAux,jugador[i].strNom);
    strcat(strAux,"#");
    strcat(strAux, strTirades);

    To get the name, you have to find the # character, and take all the characters before it.
    To get the number, you take everything after the # up to the next \0 (or 100 characters), then pass it to say atoi(), using the same base you used with itoa()
    strcpy(strAux,jugador[i].strNom);
    strcat(strAux,"#");
    strcat(strAux, strTirades);

    This is to write the winner. I want to write a string, so I'm following the next steps:

    -I copy the name to the string.
    -Add the '#'
    -Add the dice rolls.

    So I will have "Name#Dice" and ready to print.

    Another question that I have is, at the first game, there's no FILE with the winners info (obviusly, there's no winner yet), so what happens if the file is opened and there's nothing in it?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >so I'm following the next steps:
    You need to ass the null terminator for strings too!

    >strTirades[100] is enough, I think.
    Yes it is ok, but Salem did not know that information when he asked.

    >Another question that I have is, at the first game, there's no FILE with the winners info (obviusly, there's no winner yet), so what happens if the file is opened and there's nothing in it?
    But why is there a file, if there is not anything yet written in it? Maybe the file is not even opened, because it is not exists. Then FILE would be a NULL pointer. If the file exists, but it is empty, then you should read nothing(the read buffer should be empty) and take into account this possibility too.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Second Chance Algorithm
    By Aga^^ in forum C++ Programming
    Replies: 4
    Last Post: 12-23-2008, 07:03 AM
  2. First-chance exceptions.
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2008, 11:44 PM
  3. is there a c form designer out there by any chance
    By *DEAD* in forum C Programming
    Replies: 2
    Last Post: 12-16-2007, 07:48 PM
  4. any chance on finding some dram?
    By GanglyLamb in forum Tech Board
    Replies: 7
    Last Post: 04-08-2003, 10:31 AM
  5. increasing chance.
    By davee in forum C++ Programming
    Replies: 5
    Last Post: 10-23-2002, 12:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21