My appvar keeps returning really large values I did not input. I was wondering how to fix this. I am aware my code is poorly written and a mess. Thank you.

Code:


#include <tice.h>
#include <keypadc.h>
#include <graphx.h>
#include <fileioc.h>

   static void PrintTime(float elapsed)
   {
      real_t elapsed_real;
      char str[10];
      elapsed_real = os_FloatToReal(elapsed <= 0.001f ? 0.0f : elapsed);
      os_RealToStr(str, &elapsed_real, 8, 1, 2);
      os_SetCursorPos(0, 0);
      os_PutStrFull(str);
   }


    int main(void){
      char str[10];
      ti_var_t LyricAppvar;
      float LyricVal;
      float elapsed = 0;
      real_t elapsedREAL;
      
      timer_Set(1, 0);
      LyricAppvar = ti_Open("Lyrics", "r+");
      if(LyricAppvar == 0){
         os_PutStrLine("Yeah id int syudj");
         LyricAppvar = ti_Open("Lyrics", "w+");
      }
      ti_Read((void *)&LyricVal, 3, 1, LyricAppvar);
      while (!os_GetCSC());
        os_ClrHome();
      PrintTime(LyricVal);
      while (!os_GetCSC());
        //PrintTime(0.0f);
        timer_Disable(1);
        timer_Set(1, 0);
        while (!os_GetCSC());
        timer_Enable(1, TIMER_32K, TIMER_0INT, TIMER_UP);

        do
        {
            float elapsed = (float)timer_GetSafe(1, TIMER_UP) / 32768;
            PrintTime(elapsed);
        } while (!os_GetCSC());
      
      elapsed = (float)timer_GetSafe(1, TIMER_UP) / 32768;
      elapsed = elapsed * 100;
      //Rounds it off to be a 3(usually) digit number
      elapsedREAL = os_FloatToReal(elapsed <= 0.001f ? 0.0f : elapsed);
      elapsedREAL = os_RealRoundInt((const real_t *)&elapsedREAL);
      //Prints again
      os_RealToStr(str, &elapsedREAL, 8, 1, 2);
      os_SetCursorPos(5, 5);
      os_PutStrFull(str);

      LyricAppvar = ti_Open("Lyrics", "r+");
        ti_Write((const void*)&elapsedREAL, sizeof(elapsedREAL), 1, LyricAppvar);
      while (!os_GetCSC());
      timer_Disable(1);
      ti_SetArchiveStatus(true, LyricAppvar);
      ti_Close(LyricAppvar);
        return 0;
    }

You're reading and writing different types; there's no way that can work as you seem to want.

Code:
real_t elapsedREAL;
ti_Write((const void*)&elapsedREAL, sizeof(elapsedREAL), 1, LyricAppvar);

float LyricVal;
ti_Read((void *)&LyricVal, 3, 1, LyricAppvar);


And as others have already pointed out in chat, you need to read the same number of bytes as the size of the type you're reading.
Tari wrote:
You're reading and writing different types; there's no way that can work as you seem to want.

Code:
real_t elapsedREAL;
ti_Write((const void*)&elapsedREAL, sizeof(elapsedREAL), 1, LyricAppvar);

float LyricVal;
ti_Read((void *)&LyricVal, 3, 1, LyricAppvar);


And as others have already pointed out in chat, you need to read the same number of bytes as the size of the type you're reading.


Oh I see thank you so much for helping. I changed the float to a real_t, made both of the byte sizes 4, and then changed (const void *) to (void *). Now the code works as intended.
A real_t is 9 bytes in size, not 4, so I don't imagine that that will work either. You also don't have to cast to void* - the compiler converts non-void pointers to void pointers automatically when using them as function arguments.

Generally, if you're reading into a variable x, you'll want to use ti_Read(&x, sizeof x, 1, slot);, and make sure that the variable that you're writing is the same type as the variable you're reading.
  
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