Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 52
Registered: ‎01-09-2012
My Device: PlayBook
Accepted Solution

write file

Hello I'm Trying to write to a file here is an excert

FILE* f= fopen("/shared/documents/save.txt", "w");

 

if(f==NULL){

 

fprintf(stderr,"Cannot create output.txt...\n");

 

_exit(1);

}

fprintf(f,"Hello World");

 

close(f);

 

The error i keep getting is Cannot create output.txt Why doesn't it write to that location ?

& If i write to

fopen("./app/native/save.txt", "w"); It writes the file but i'm unable to find the location on the device.
What am i missing as to why this writes an the other doesnot ?

 

Thanks

 

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: write file

You can't write to /shared, as there's no such folder. There is, however, when your app is running, a symlink (to the shared folder) inside your current directory, as "./shared". Use that. You can simply leave off the prefix too, and use "shared" without a leading slash or dot-slash.

I don't know what's happening with the "./app" thing except that it really can't be working like that as your app has no access to create or write to such a folder, except conceivably in devmode on the simulator only. I thought even then it was a read-only folder, so I suspect you're simply seeing a save.txt file that you've managed to bundle into your .bar file and install with the app, and you're not really writing it, but maybe it's something else...

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
Posts: 52
Registered: ‎01-09-2012
My Device: PlayBook

Re: write file

Thanks for the reply however i've tried it and it still doesn't work.
FILE* f= fopen("shared/documents/save.txt", "w");

FILE* f= fopen("shared/save.txt", "w");

 

I have reboooted the device as well

 

I'm using 2.0 Beta 3

Version: 2.0.0

Build id: 201201122052

Device OS Version: 2.0.0.7971

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: write file

Do you request/grant 'Files' permissions in bar-descriptor.xml?

What error do you get?

New Developer
Posts: 52
Registered: ‎01-09-2012
My Device: PlayBook

Re: write file

That part i overlooked.

Thanks to all those who replyed .

 

Much appreciated

 

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: write file

By the way, the shared/save.txt one would never work either, as you may not write directly to the shared/ folder even with the permission, nor create folders right there. You can read, write, create folders, or delete stuff from any of the subfolders of shared, however.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!