even more problems working with files [obsolete]

Posted on
  • Hello again,

    it might not be related to my previous post which is why I created another one.

    StorageFile.read() seems to always expect a length argument - but how do I then read a whole file? there is no size method?

    But there is another problem: when I try

    const Storage = require('Storage');
      function compareFileWith (expectedContent) {
        let readText = Storage.open('4testing','r').read(1000);­
        if (readText != expectedContent) {
          print('>>>> file does not look as expected');
          print('>>>> expected "' + expectedContent + '"');
          print('>>>> got "' + readText + '"');
        }
      }
    
    /**** create file ****/
    
      if (Storage.write('4testing','Hello, World!')) {
        print('file "4testing" was successfully created');
      } else {
        print('file "4testing" could not be created');
      }
      compareFileWith('Hello, World!');
    print('finished');
    

    the file "4testing" is successfully created - but reading it using StorageFile.read produces a strange result:

    file "4testing" was successfully created
    >>>> file does not look as expected
    >>>> expected "Hello, World!"
    >>>> got "4testing4testing4testing"
    finished
    

    What am I doing wrong?

    BTW: I am using the "cutting edge" distribution of today

  • As I already wrote: I found my mistake myself. I should have used

      let TestFile = Storage.open('4testing','w');
      if (TestFile.write('Hello, World!')) {
        print('file "4testing" was successfully created');
      } else {
        print('file "4testing" could not be created');
      }
    

    for creating the file.

    Sorry for bothering you!

  • Thr 2020.01.09

    For those seeking the reference link for read and write

    https://banglejs.com/reference#Storage
    https://banglejs.com/reference#StorageFi­le

    Using an offset into that file data rather than overwrite

    https://banglejs.com/reference#l_Storage­_write

    other suggested use snippet examples

    http://forum.espruino.com/conversations/­329768/

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

even more problems working with files [obsolete]

Posted by Avatar for Andreas_Rozek @Andreas_Rozek

Actions