Announcement

Collapse
No announcement yet.

file_Read return value trouble

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

  • file_Read return value trouble

    Hi,
    I'm having trouble with the return value of file_Read function,
    when I read from a file with a dimension that is less then my destination buffer, file_Read function always return the dimension of my buffer (FILEBUFFSIZE) instead of number of bytes read, Is it normal?
    This is the code I'm using:

    FILEBUFFSIZE 0x100

    uiFile_PackByteNum := file_Read( FileBuff , FILEBUFFSIZE, TransfFileHnd);

    Example: If the file has a dimension of 62 bytes file_Read returns 256 (FILEBUFFSIZE) instead of 62.

    Thanks

  • #2
    Which PmmC version are you using?

    I just tried this with a 62 byte file, and also a 318 byte file and it behaved correctly with both

    Code:
    #platform "uLCD-35DT"
    
    #inherit "4DGL_16bitColours.fnc"
    
    var buf[128] ;
    
    func main()
        var disk, hndl, i ;
        putstr("Mounting...\n");
        if (!(disk:=file_Mount()))
            while(!(disk :=file_Mount()))
                putstr("Drive not mounted...");
                pause(200);
                gfx_Cls();
                pause(200);
            wend
        endif
        hndl := file_Open("62b.txt", 'r') ;
    //    hndl := file_Open("318b.txt", 'r') ;
        if (!hndl)
            print("Open failed ", file_Error()) ;
            repeat forever
        endif
    
        while(!file_Error())
            i := file_Read(buf,256,hndl) ;
            print("Bytes read=", i, "\n") ;
        wend
        print("Reading ended with ",file_Error()) ;
        file_Close(hndl) ;
    
        repeat
        forever
    
    endfunc
    Mark

    Comment


    • #3
      Hi Mark,

      Thank you for your answer.
      I think I found the reason for my trouble (I hope so)...
      In my program I open the file and I do a CRC16 calculation on the entire file data (see the code below) , doing this operation the end of file is reached and it seems that after this operation the file_Read always returns 256 (of course I do a file_Rewind before using file_Read).
      Take a look at my code below ant tell me if you find somethink wrong.

      ...........
      // Try to open the file and check for errors
      TransfFileHnd := file_Open(str_Ptr(File_FileName), 'r');
      fTransfError := file_Error();

      if(fTransfError == FE_OK)
      // Read the file size
      file_Size( TransfFileHnd, &lFile_FileLen[1], &lFile_FileLen[0]);


      uiFile_Crc := vCalcFileCRC(); // File CRC calculation

      // Read from file and fill the buffer
      uiFile_PackByteNum := file_Read( FileBuff , FILEBUFFSIZE, TransfFileHnd);
      gfx_MoveTo(600,459 );print(uiFile_PackByteNum);pause(1000); // This code is just for debug
      endif
      ............


      func vCalcFileCRC()
      var locCrc := 0;
      var n := 1;

      file_Seek(TransfFileHnd, 0, 0);

      while(file_Error() != FE_EOF && n != 0)
      n := file_Read( FileBuff , FILEBUFFSIZE, TransfFileHnd);
      locCrc := crc_CCITT(str_Ptr(FileBuff), n, locCrc);
      wend
      file_Seek(TransfFileHnd, 0, 0);

      return locCrc;
      endfunc



      Thanks.
      Claudio



      Comment


      • #4
        Hmm, the problem is a bug in file_Seek that results in an incorrect value being set for files < 512 bytes long.

        A quick workaround for you with the current PmmC is to save TransfFileHnd[FILE_CURR_SECTOR_TOP] just after opening the file and then restoring it after each seek to the start
        Mark

        Comment


        • #5
          Ok Mark,

          yesterday, before your answer, I solved the problem closing the file after CRC calculation and then re opening it, it's not an elegant solution but it works!
          Now I will use your workoraund.

          Thanks.

          Claudio.

          Comment

          Working...
          X