Announcement

Collapse
No announcement yet.

[Update: BUG][Workaround Found]Reading and writing to files not working properly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Update: BUG][Workaround Found]Reading and writing to files not working properly

    Hey guys, I am relatively new to the world of coding so this may be a simple fix, but so far I haven't been able to figure this out.
    My program is essentially taking data from the serial port, storing it in a file, then reading it from that file when I want to display that data.
    The problem is that when I read and display the data I stored in the file it isn't the same as what I was attempting to store. Now, if the file I am writing to doesn't exist and is created by the program it stores the data fine the first time. It's only when I'm appending to an existing file that the problem arises. I am using the first space in the file to store how many sets of data I have.
    If you run this program you can see the problem by going into the compass page, hitting store at least twice, and then going back to the SD read page where it is displaying the data it stored.
    I am using a uOLED-32028-P1_GFX2
    I uploaded the full program and here are the relevant functions[HTML]
    func AddLine(var XX, var YY, var heading)
    lineStorageCount++; lineStorageX[lineStorageCount] := XX; lineStorageY[lineStorageCount] := YY; lineStorageHeading[lineStorageCount] := heading-90;

    // ############################## // ### STORING NEW LINE ON SD ### // ############################## if (!file_Exists("storage5.txt")) variableFile := file_Open("storage5.txt", 'a');
    file_PutW(lineStorageCount, variableFile);
    file_PutW(lineStorageX[lineStorageCount], variableFile); file_PutW(lineStorageY[lineStorageCount], variableFile); file_PutW(lineStorageHeading[lineStorageCount], variableFile);
    else
    variableFile := file_Open("storage5.txt", 'a');
    var HIptr, LOptr, res;
    res := file_Tell(variableFile, &HIptr, &LOptr) ;
    txt_Set(TEXT_COLOUR, WHITE); gfx_MoveTo(0,0); print("HI", HIptr, ";", "LO", LOptr, ";", res);

    file_PutW(lineStorageX[lineStorageCount], variableFile); txt_Set(TEXT_COLOUR, WHITE); gfx_MoveTo(0,0); //print(lineStorageX[lineStorageCount]); file_PutW(lineStorageY[lineStorageCount], variableFile); file_PutW(lineStorageHeading[lineStorageCount], variableFile);
    gfx_MoveTo(0,250); print(file_Error());
    file_Rewind(variableFile); file_PutW(lineStorageCount, variableFile);



    endif
    file_Close(variableFile);
    variableFile := file_Open("storage5.txt", 'a');
    //print(file_GetW(variableFile)); //var HIptr, LOptr, res;
    res := file_Tell(variableFile, &HIptr, &LOptr) ;
    txt_Set(TEXT_COLOUR, WHITE); gfx_MoveTo(0,0); //print("HI", HIptr, ";", "LO", LOptr, ";", res);
    file_Close(variableFile);





    //gfx_MoveTo(0,0); //print(lineStorageX[lineStorageCount],lineStorageY[lineStorageCount],lineStorageHeading[lineStorageCount]);


    endfunc
    func SetupSDRead()
    gfx_Cls();
    if (file_Exists("storage5.txt")) print("Reading... "); var i; variableFile := file_Open("storage5.txt", 'r'); file_Rewind(variableFile); lineStorageCount := file_GetW(variableFile); print(lineStorageCount, ";"); i := 1; while(i

  • #2


    Soo... I found out that the file_Rewind() function for some reason screws up any values you have written since the last time you opened the file. My solution was to simply close the file then reopen and continue writing to the file. Seems like a bug to me, hopefully the 4dgl guys can fix it. Hope this helps someone.

    Comment


    • #3


      I haven't tried running your program but I spotted a few errors that I think may be causing your problems.



      If a file doesn't exist you should open it with mode 'w'.



      The main problem is that a file can only be written to sequentially, or appended to sequentially. The file system is not sophisticated enough to allow you to rewind it (or seek for that matter) and write to another position, rewind and seek are meant for reading only.
      Mark

      Comment


      • #4


        really??? I use this code, and it seems to be working (stores my variables for use in the next program loaded from the uSD..)

        HTML Code:
            to(csvBuffer); print([HEX2] I2C_readbyte(M41T81,MNTH),[HEX2] I2C_readbyte(M41T81,DOM),"info.txt");
        ptr:=str_Ptr(csvBuffer);
        sessionFile:=file_Open(ptr,'a');
        if(sessionFile)
        file_Rewind(sessionFile); //go back to the start
        file_PutW(totalEnergyCal, sessionFile);
        file_PutW(active_time,sessionFile);
        file_PutW(rest_time,sessionFile);
        file_PutW(0x30,sessionFile);
        
        endif
        file_Close(sessionFile);
        Maybe it works because it is always the same filesize?

        If we are not supposed to use file_Rewind() and file_Seek() how are we supposed to edit configuration files and pass user config data between programs?

        Andy
        MicroLec Industries P/L
        www.intelligym.com.au

        Comment


        • #5


          Yes, it does actually work correctly because it is the same filesize, so seeking to a lower address is fine in append mode (although its an undocumented 'feature')
          Currently the filesize cannot be expanded in append mode and will simply result in truncation to existing filesize.
          Regards,
          Dave

          Comment


          • #6


            yay... bonus feature.. Noted that files of growing size can only be appended, otherwise I guess we would need to open two files, read / write from one to the other, then continue on and erase the first file.. or something like it, if we were going to edit contents with growing filesize...

            Anyways thanks for the clarification

            Andy
            MicroLec Industries P/L
            www.intelligym.com.au

            Comment


            • #7


              Yes, just to clarify my omission, "Currently the filesize cannot be expanded in append mode using file_Seek(..) - seek will truncate to current file size"
              Regards,
              Dave

              Comment

              Working...
              X