Storage file read/write problem [fixed]

Posted on
  • Good morning!

    I must be completely blind or dumb in a different way, but when I write a string into a file, read it back into another string and then compare both strings with "!==", they are reported as different!

    Then, if I compare them character by character, no difference is found (as expected).

    Does anybody have a solution for this puzzle?

    The gist may be directly loaded and run on a connected watch through the IDE. Here comes the code:

    const Storage = require('Storage');
    function compareStrings (Original,Candidate) {
      let OriginalLength  = Original.length;
      let CandidateLength = Candidate.length;
      if (CandidateLength > OriginalLength) {
        print('>>>> more characters read than written');
        print('>>>> first additional character: ' + Candidate.charCodeAt(OriginalLength));
      if (CandidateLength < OriginalLength) {
        print('>>>> less characters read than written');
        print('>>>> first missing character: ' + Original.charCodeAt(CandidateLength));
      for (let i = 0; i < OriginalLength; i++) {
        if (Original[i] !== Candidate[i]) {
          print('>>>> different characters read than written');
          print('>>>> first mismatch at #' + i + ': expected ' + Original.charCodeAt(i) + ', got ' + Candidate.charCodeAt(i));
      print('>>>> same characters read as written');
    const FileContent_1 = 'this file exists for testing purposes only';
    const FileContent_2 = 'this file still exists for testing purposes only';
    if (Storage.write('4testing',FileContent_1)­) {
      print('file "4testing" was successfully created');
    } else {
      print('file "4testing" could not be written');
    if ('4testing') !== FileContent_1) {
      print('contents of file "4testing" do not look as expected');
      print('(namely "' +'4testing') + '")');
    if (Storage.write('4testing',FileContent_2)­) {
      print('file "4testing" was successfully overwritten');
    } else {
      print('file "4testing" could not be overwritten');
    if ('4testing') !== FileContent_2) {
      print('contents of file "4testing" do no longer look as expected');
      print('(namely "' +'4testing') + '")');
    print('file "4testing" has finally been deleted');
  • Well,

    I just found a solution: if I replace

    if ('4testing') !== FileContent_1) {


    if ('''4testing') !== FileContent_1) {

    (and similar for the second read operation) everything runs as expected...

    @Gordon may it be that I found a severe bug?

  • @Andreas_Rozek, what was the output of your test program?

    • length differences
    • character differences

    How did you compare character by character?

    My only explanation would be that the data read is not a string but concatenated to an empty string .toString() is applied and makes it what you expect... weird and not really reasonable... but JS is known for doing (sometimes) some conversion...

  • That's what looked so weird: "!==" was true (mind the extra "=" which means "without any conversion"), but when comparing lengths and character codes, everything was equal.

    Thus, I could

    • not compare the outcome of with a string, but
    • use .length and .charCodeAt of the outcome of without any problems and
    • compare the outcome of with a string after appending to ''
  • Thanks. It actually boils down to this:

    >E.toString("this is a test of string compare")==="this is a test of string compare"

    If you used != / == then you'd be fine, but Espruino's doing a 'type compare' and it gets fooled by having a 'flat string' and a string. That's a nasty one - should get a fix in for it soon.

  • ohh...VERY good to know!

    Thanks a lot!

  • Ok, just fixed :)

  • that's been quick, thanks!

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

Storage file read/write problem [fixed]

Posted by Avatar for Andreas_Rozek @Andreas_Rozek