So, I am trying to save a struct to an appvar. However, this results in every value overflowing and just straight up going crazy. Code for Saving/Loading:


Code:
int load(void *Player, void *Counts, void *Prices)
{
   uint8_t var = ti_Open("CEClickS", "r");
   if (var == 0)
   {
      return 1;
   }
   if (ti_GetC(var) != 'a')
   {
      return 1;
   }
   if (ti_Read(Player, sizeof((*Player)), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'b')
   {
      return 1;
   }
   if (ti_Read(Counts, sizeof((*Counts)), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'c')
   {
      return 1;
   }
   if (ti_Read(Prices, sizeof((*Prices)), 1, var) != 1)
   {
      return 1;
   }
   ti_Close(var);

   return 0;

}
int save(void* Player, void* Counts, void* Prices)
{
   uint8_t var = ti_Open("CEClickS", "w");
   if (ti_PutC('a', var) == EOF)
   {
      os_PutStrFull("a");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Player, sizeof((*Player)), 1, var) != 1)
   {
      os_PutStrFull("Player");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('b', var) == EOF)
   {
      os_PutStrFull("b");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Counts, sizeof((*Counts)), 1, var) != 1)
   {
      os_PutStrFull("Count");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('c', var) == EOF)
   {
      os_PutStrFull("c");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Prices, sizeof((*Prices)), 1, var) != 1)
   {
      os_PutStrFull("Price");
      ti_Close(var);
      return 1;
   }
   ti_SetArchiveStatus(true, var);
   ti_Close(var);
   return 0;
}


Structs:


Code:

struct player {
   uint64_t points;
   uint64_t cps;
   uint64_t clickvalue;
   uint64_t cps_mult;
   uint64_t cvm;
   uint8_t unlocked;
   uint8_t page;
};

struct counts {
   uint64_t addcount, studentcount, subcount, cookiecount, multcount, officecount, divcount, phonecount, expcount, spacecount, calccount, buttoncount;
};

struct prices
{
   uint64_t addprice;
   uint64_t studentprice;
   uint64_t subprice;
   uint64_t cookieprice;
   uint64_t multprice;
   uint64_t officeprice;
   uint64_t divprice;
   uint64_t phoneprice;
   uint64_t expprice;
   uint64_t spaceprice;
   uint64_t calcprice;
   uint64_t buttonprice;
};


Thanks for any help Very Happy !
I'm not sure what you are trying to make, but this is what I came up with.

It looks like you aren't defining the struct that you want to save, but rather saving the struct type. I don't know if that makes sense. But here is an example:

I define my player structure here:

Code:
struct player_t {
   uint64_t points;
   uint64_t cps;
   uint64_t clickvalue;
   uint64_t cps_mult;
   uint64_t cvm;
   uint8_t unlocked;
   uint8_t page;
};

#define your structure here (This can be done in the .c file.)
struct player_t player;

#define your structure here (This can be done in the .h file.)
extern struct player_t player;



Now that you have defined your structures, you need to save and load them from an appvar.

Code:
int load(void *Player, void *Counts, void *Prices)
{
   uint8_t var = ti_Open("CEClickS", "r");
   if (var == 0)
   {
      return 1;
   }
   if (ti_GetC(var) != 'a')
   {
      return 1;
   }
   if (ti_Read(Player, sizeof(struct player_t), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'b')
   {
      return 1;
   }
   if (ti_Read(Counts, sizeof((struct count_t)), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'c')
   {
      return 1;
   }
   if (ti_Read(Prices, sizeof((struct prices_t)), 1, var) != 1)
   {
      return 1;
   }
   ti_Close(var);

   return 0;

}
int save(void* Player, void* Counts, void* Prices)
{
   uint8_t var = ti_Open("CEClickS", "w");
   if (ti_PutC('a', var) == EOF)
   {
      os_PutStrFull("a");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Player, sizeof(struct player_t), 1, var) != 1)
   {
      os_PutStrFull("Player");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('b', var) == EOF)
   {
      os_PutStrFull("b");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Counts, sizeof(struct count_t), 1, var) != 1)
   {
      os_PutStrFull("Count");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('c', var) == EOF)
   {
      os_PutStrFull("c");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Prices, sizeof(struct prices_t), 1, var) != 1)
   {
      os_PutStrFull("Price");
      ti_Close(var);
      return 1;
   }
   ti_SetArchiveStatus(true, var);
   ti_Close(var);
   return 0;
}


Let me know if you have any other question I'll be happy to help! Smile
I tried the new save/load code, but it appears some values are set to zero, leading to some issues. (Clickvalue or CVM is 1 on save, on next load equal to zero)
Since your struct is full of primitive data types, you can store and restore the entire structure in one fell swoop; no need to do all this field-by-field stuff.


Code:
int load(player_t* player, counts_t* counts, prices_t* prices) {
   uint8_t avHandle = ti_Open("CEClickS", "r");
   if (0 == avHandle) {
      return -1;
   }
   if (1 != ti_Read(player, sizeof(player_t), 1, avHandle)) {
      return -2;
   }
   if (1 != ti_Read(counts, sizeof(counts_t), 1, avHandle)) {
      return -2;
   }
   if (1 != ti_Read(prices, sizeof(prices_t), 1, avHandle)) {
      return -2;
   }
   ti_Close(avHandle);
   return 0;
}

int save(player_t* player, counts_t* counts, prices_t* prices) {
   uint8_t avHandle = ti_Open("CEClickS", "w");
   if (0 == avHandle) {
      return 1;
   }
   if (1 != ti_Write(player, sizeof(player_t), 1, avHandle)) {
      return -2;
   }
   if (1 != ti_Write(counts, sizeof(counts_t), 1, avHandle)) {
      return -2;
   }
   if (1 != ti_Write(prices, sizeof(prices_t), 1, avHandle)) {
      return -2;
   }
   return 0;
}


Edit: I suppose you could put those extra safety characters Alvajoy is suggesting; that certainly adds additional assurances.
My code right now is:

Code:

int load(struct player_t* Player, struct counts_t* Counts, struct prices_t* Prices)
{
   uint8_t var = ti_Open("CEClickS", "r");
   if (var == 0)
   {
      return 1;
   }
   if (ti_GetC(var) != 'a')
   {
      return 1;
   }
   if (ti_Read(Player, sizeof(struct player_t), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'b')
   {
      return 1;
   }
   if (ti_Read(Counts, sizeof(struct counts_t), 1, var) != 1)
   {
      return 1;
   }
   if (ti_GetC(var) != 'c')
   {
      return 1;
   }
   if (ti_Read(Prices, sizeof(struct prices_t), 1, var) != 1)
   {
      return 1;
   }
   ti_Close(var);

   return 0;

}
int save(struct player_t* Player, struct counts_t* Counts, struct prices_t* Prices)
{
   if ((*Player).clickvalue == 0)
   {
      return 1;
   }
   uint8_t var = ti_Open("CEClickS", "w");
   if (ti_PutC('a', var) == EOF)
   {
      os_PutStrFull("a");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Player, sizeof(struct player_t), 1, var) != 1)
   {
      os_PutStrFull("Player");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('b', var) == EOF)
   {
      os_PutStrFull("b");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Counts, sizeof(struct counts_t), 1, var) != 1)
   {
      os_PutStrFull("Count");
      ti_Close(var);
      return 1;
   }
   if (ti_PutC('c', var) == EOF)
   {
      os_PutStrFull("c");
      ti_Close(var);
      return 1;
   }
   if (ti_Write(Prices, sizeof(struct prices_t), 1, var) != 1)
   {
      os_PutStrFull("Price");
      ti_Close(var);
      return 1;
   }
   ti_SetArchiveStatus(true, var);
   ti_Close(var);
   return 0;
}

Thank you, I've gotten it working now!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement