Announcement

Collapse
No announcement yet.

Writing to / Reading from File

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

  • Writing to / Reading from File

    Hi there,

    I have a curious issue whith writing a buffer onto a file on SD and reading it from that file later: The result is not the same, but I can't figure out by no means what the reason is. I have "condensed" the code (below) for better clarification, and hope that someone can help me. The system is an uLCD32PTU.
    BTW: There are no file_errors().

    With best regards,
    Peter



    Code:
    #platform "uLCD-32PTU"
    #inherit "4DGL_16bitColours.fnc"
    #inherit "VisualConst.inc"
    
    func main()
    //---------------------------------------------------------------- Check if SD card mounted
    if (!(file_Mount())) while(!(file_Mount())) putstr("Drive not mounted..."); wend endif gfx_Set(SCREEN_MODE,LANDSCAPE); gfx_Cls();
    //---------------------------------------------------------------- Variables (16 bits only!)
    var hndl1, hndl2, res, i, count, sizeH, sizeL; var WrBuffer [] := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; var RdBuffer[16];
    //---------------------------------------------------------------- WRITE BUFFER TO FILE:
    res := file_Erase("test.dat"); hndl1 := file_Open("test.dat",'w'); res:=file_Size(hndl1,&sizeH,&sizeL); print("\nFile size: ",sizeH,sizeL," bytes\n"); count :=file_Write(WrBuffer,32,hndl1); res:=file_Size(hndl1,&sizeH,&sizeL); print("\nFile size: ",sizeH,sizeL," bytes\n"); res := file_Close(hndl1);
    //---------------------------------------------------------------- READ BUFFER FROM FILE:
    hndl2 := file_Open("test.dat",'r'); res:=file_Size(hndl2,sizeH,sizeL); print("\nFile size: ",&sizeH,&sizeL," bytes\n"); count :=file_Read(RdBuffer,32,hndl2); print(count,"\n"); for(i:=0;i<16;i++) print(RdBuffer[i]>>8,","); res := file_Close(hndl2); repeat forever
    endfunc

    The printout yields:
    File size: 00 bytes (OK)
    File size: 032 bytes (OK)
    File size: 1314 bytes (NOT OK, but the file size is OK when checked on a PC)
    210,0,1,0,0,0,0,1,2,3,4,5,6,7,8,9, (NOT OK, same when checked on a PC)

    The first number (210) changes if the program is modified (e.g, additional lines)
    Last edited by Vince_4D; 24th April 2018, 11:18 AM. Reason: Code Format changed for better readability - Vince

  • #2
    Hi spaceman, Welcome to the forum!

    I tested your code and found few problems in implementing the functions.

    Below is a modified code based on the code you sent.

    Note: Read the comments to be guided in implementing this program.

    Hope this will help.

    Code:
    func main()
    //---------------------------------------------------------------- Check if SD card mounted
        if (!(file_Mount()))
            while(!(file_Mount()))
                putstr("Drive not mounted...");
            wend
        endif
        gfx_Set(SCREEN_MODE,LANDSCAPE);
        gfx_Cls();
     //---------------------------------------------------------------- Variables (16 bits only!)
        var hndl1, hndl2, res, i, count, sizeH, sizeL;
        var WrBuffer[] := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
        var RdBuffer[16];
     //---------------------------------------------------------------- WRITE BUFFER TO FILE:
        var Result[2];
        var hiWord[2];
        var loWord[2];
        var p;
    
        res := file_Erase("test.dat");
        hndl1 := file_Open("test.dat",'w');
    
        var ptr;
        ptr := str_Ptr(WrBuffer);
        count := file_Write(ptr,32,hndl1);      // ptr is used to put the data at the start loc
    
        res:=file_Size(hndl1,&sizeH,&sizeL);  // sizeH and sizeL are 16bit data
    
        hiWord[0] := 0;
        hiWord[1] := sizeH;     // hiWord 32 bit (0000000000000000 xxxxxxxxxxxxxxxx)
    
        loWord[0] := sizeL;
        loWord[1] := 0;         // loWord 32 bit (xxxxxxxxxxxxxxxx 0000000000000000)
    
    
        uadd_3232(Result, hiWord, loWord); // adds 2 32-bit values  - refer to manual
        p := str_Ptr(Result);
    
        print("File size: ");
        str_Printf(&p, "%lu");       // prints long unsigned decimal of the result - refer to manual
        print(" bytes\n");
    
        res := file_Close(hndl1);
     //---------------------------------------------------------------- READ BUFFER FROM FILE:
        hndl2 := file_Open("test.dat",'r');
        sizeH := 0;
        sizeL := 0;
        res:=file_Size(hndl2,&sizeH,&sizeL);
    
        hiWord[1] := sizeH;
        loWord[0] := sizeL;
    
        uadd_3232(Result, hiWord, loWord);
        p := str_Ptr(Result);
    
        print("File size: ");
        str_Printf(&p, "%lu");
        print(" bytes\n");
    
        count :=file_Read(RdBuffer,32,hndl2);
        print("Count: ",count,"\n");
    
        for(i:=0;i<16;i++)
            if (i == 15)
                print(RdBuffer[i]);
            else
                print(RdBuffer[i],",");
            endif
        next
        res := file_Close(hndl2);
    
        repeat
        forever
     endfunc
    Best regards,

    - Vince -
    Vince_4D

    Comment


    • #3
      Dear Vince,

      Thank you very much, this help!
      I haven't read the manual ("file_write",page 220) carefully enough. My fault, sorry for that.

      Best regards,
      Peter

      Comment


      • #4
        I had the same issue and using the str_Ptr() on the write fixed it. Why does the write need a byte pointer and the read does not? The documentation says nothing about using a byte pointer.

        Comment


        • #5
          Hi,

          The manual did say that the source argument of file_Write() command is a byte-aligned string pointer as shown below.


          Click image for larger version

Name:	Capture.PNG
Views:	63
Size:	39.2 KB
ID:	64506


          But for additional information, below is the explanation:
          • A “Write” can be from RAM or Flash, but since flash is byte aligned, the “Write” needs to be byte aligned to enable.
          • A “Read” can only be to RAM, so the byte alignment is not required.
          Hope this will help.


          Best regards,
          Vince_4D

          Comment


          • #6
            That’s NOT what the Diablo documentation says. See screenshot.
            Also that “additional tidbit" of information should be included in the documentation as well since we are not mind readers.
            Attached Files
            Last edited by dskogman; 18th July 2018, 12:04 AM.

            Comment


            • #7
              Hi,

              Apologies for the confusion.
              The one that I referred in the previous post is from Picaso reference manual. The issue is now being addressed by the documentation team and will update the document as soon as possible.
              Thank you for your recommendation.

              Kindest regards,
              Vince_4D

              Comment

              Working...
              X