An archive of community.esquilo.io as of Saturday January 26, 2019.

File IO Explanation

benhaubin

I'm really confused on the file IO in Squirrel. I want to store a JSON document into a file, and then retrieve it later. Can someone explain how to write and retrieve an entire file as a string (if it's possible)?

patrick

Here's an example:

require("JSON");

test <- {a=1, b="two"};

print(jsonencode(test) + "\n");

// Open a file on the micro SD
const FILENAME = "sd:/test.json";

// create and/or open for write
f <- file(FILENAME, "w+");

f.writestr(jsonencode(test));

f.close();

// open the file for read
f = file(FILENAME, "r");

teststr <- f.readstr(f.len());
print(teststr + "\n");

test = jsondecode(teststr);

print(test.a + " " + test.b + "\n");

f.close();

Note: After writing the file, if you refresh the file list view in the IDE you should see, and be able to edit the new test.json file.

This post may also be helpful:

benhaubin

Thanks so much! I finally somewhat understand it now, all the guide I could find on the internet for io in squerll confused me beyond belief.