saveStrings() saves cryptic text

Yesterday I had success using saveStrings() but when I copy the code into my current script, it saves gibberish in the file.

console.log() shows the output string is forming correctly but the saved file contains only "∫w^~)fi" (without quotes).

Here's the code causing trouble (note gridSquare is an array)...

   function createData() {
      dataList = startX + "," + startY + ",";
      dataList = dataList + gridSquare;
      list = split(dataList, ',');
    }


    function writeData() {
      var fileName = "data" + fileNum + ".txt"
      saveStrings(list, fileName);
      console.log(list);
    } 

Since the list string looks good and the file is generated, I'm at a loss as to why the data hasn't saved as it does with the standalone script.

EDIT: It seems to work from Firefox but not from the P5 editor.

Tagged:

Answers

  • I don't think that answers it. I'm using the standard P5 index.html template (with a few added .js entries) so I assume it is not a UTF issue. Plus it works from the standalone script, and it works in Firefox. I tried Chrome but only got as far as "Loading..." I'll have to check that out tomorrow (it is image intensive).

    Maybe a restart will fix it :) 1am here now, so off to bed. Thanks.

  • edited October 2016

    UPDATE:

    I saved a copy of the file and started testing by commenting out lumps of code until a simple file saved correctly.

    saveStrings("some text", "data.txt");

    It worked until I reinstated the loadStrings() call in preload().

    dataArray = loadStrings('data2.txt');

    Then I got gibberish again in the output (in a 6-byte file). Interestingly, it doesn't prompt to save when it fails, but does prompt when it works.

    And...it works if I take the loadStrings line out of preload() and put it in setup() - and yet the in/out files have nothing to do with each other.

    It works fine in Firefox, so it looks like there's some issue with the P5 editor but I'm a bit new to make that call. I can't test in Chrome because I get the "Cross Origin" error loading a local image file, so I gave up.

  • saveStrings("some text", "data.txt");

    I've already explained to you that "some text" isn't an Array, but a String: L-)
    https://forum.Processing.org/two/discussion/18554/savestrings-makes-file-one-character-per-line-p5-js#Item_4

    And that saveStrings() demands its 1st argument to be an Array:
    http://p5js.org/reference/#/p5/saveStrings

    Fixed call: saveStrings([ 'some text' ], 'data.txt'); O:-)

  • I can't test in Chrome because I get the "Cross Origin" error loading a local image file, ...

    Use "--allow-file-access-from-files" at your Chrome-based browser's shortcut:
    http://chrome-allow-file-access-from-file.com/

  • edited October 2016

    I'll consider myself suitably chastised - but that's not the cause of the problem :)

    The following code does not work for me in the P5 editor (OS X 10.10.5, P5 0.6.1) unless I comment out the loadStrings line. Note that the load and save are not related to each other...

    function preload(){
        data = loadStrings('data2.txt')
    }
    
    function setup() {
         saveStrings(["some text"], "data1.txt");
    }
    
    function draw() {
    
    }
    
  • edited October 2016

    ... so it looks like there's some issue with the P5 editor...

    I've never used that IDE. And generally it's not up-to-date most of the time.
    I prefer an online IDE for it though: :D

    1. http://p5js-SandBox.HerokuApp.com/
    2. http://p5js.SketchPad.cc/

    When running offline, pay attention to the ".html" file.
    It determines which charset is allowed for all JS programs. 8-X

  • edited October 2016

    Very confusing... In preload() you load 'data2.txt'. But you save as 'data1.txt' in setup(). @-)

  • Yes, they are different things. They weren't always different things but I changed them for this simple test to see if there was some conflict with trying to save over the loaded file. It made no difference what they were called. For some reason, loadStrings messes with saveStrings - but only in the P5 editor. It works fine in Firefox.

    I noticed I used single and double quotes- but this isn't the issue either (I'm not sure if either is preferred though).

  • edited October 2016

    loadStrings() messes with saveStrings() - but only in the P5 editor.

    Like I've warned about, avoid the p5.js editor for now. :-&

    I'm not sure if either is preferred though.

    In JS we can use double quotes "", single quotes '' and even back-ticks `` for string literals! :>

  • edited October 2016

    With regards to the Google Chrome issue, I found a Mac solution but it still won't open the program - although it no longer shows any errors at all.

Sign In or Register to comment.