Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

File access - strange behaviour

ANSWERED

Hi everybody,

 

I try to write and read some app settings to a file on my ionic.

 

While the reading with

 

fs.readFileSync("/mnt/assets/resources/myFile.ini", "utf-8");

 

works without problems the write command

 

fs.writeFileSync("/mnt/assets/resources/myFile.ini", tmpString, "utf-8");

 

within the same app doesn't work.

 

The write command always shows the error "Couldn't open file: /mnt/assets/resources/myFile.ini" while the read command just a few lines later work with the same file and filename...

 

Does anybody have an idea?

 

Capitano

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Have you tried without the path, just the filename.

It works for me (using cbor)

View best answer in original post

Best Answer
8 REPLIES 8

Not sure why but the examples use .txt extensions even when they should be .json

There may be a limitation there, does switching away from .ini make any difference?

Best Answer
0 Votes

yes - I also tried the examples literally already - with filetype ascii and name "ascii.txt" - brings the same error, too.

 

I wondered if it's necessary to grant access for writing to the device, but couldn't find anything in this direction.

I also wondered if it could be possible to create a file in the code or if it would be created by using the writeFileSync-function if it doesn't exist - - but as mentioned a these thoughts are theoratical - writing doesn't work at all.

 

Ashampoo_Snap_2017.12.04_19h21m33s_001_.jpg

Best Answer
0 Votes

Have you tried without the path, just the filename.

It works for me (using cbor)

Best Answer

As suggested by @Argy, remove the full path: just let resources/ as by default files will be written in /mnt/assets/. It will also work for the reading the file.

Best Answer
0 Votes

Hello all,

 

just a short feedback: In my app now the suggestion of @agsurf5 works with both the writing and the reading. Unfortunately all combinations with a part of the filepath don't work - also the "resources/myFile.ini" naming doesn't.

 

Thanks a lot

 

Capitano

Best Answer

The relative path (start at project roots) should work for reading and writing. As an example, if I create a folder named data in the resources folder, I will be able to access it with

let utf8_read = fs.readFileSync("resources/data/myFile.ini", "utf-8");

If it's still not working, feel free to send me the export of your project in private and I'll give it a closer look.

Best Answer
0 Votes

The relative path *should* work (and might now in the more recent firmware), but I had to do this to access a file:-

var stats = fs.statSync("/mnt/assets/resources/level_data.bin");
Best Answer
0 Votes

Hi,

I know this topic is old and marked as solved but I'd just like to mention another little (non obvious) undocumented feature I just discovered that will cause the exact same issue,

Anything you put in your app's resources folder that gets copied into the finished zip is write-protected. And it's impossible to override this protection in code.

So, if you want to have a little settings.txt file to read from and write to, you must initially generate it in code. Otherwise you'll never get write access to your file.

Hope this helps someone.

 

Best Answer