Allegro 5, android, writing files.
Ian Lewis

Hello All,

I'm (still) porting an A5 game I wrote for RaspberryPi and Windows to Android. My current difficulty is creating/writing files. Hi score tables, saved config, that sort of thing.

As per this thread ( https://www.allegro.cc/forums/thread/617058 ) I learned that I need to call al_android_set_apk_file_interface() in order to load any assets, but according to the manual this gives read-only access.

I tried the following, hoping to write a file somewhere:

#SelectExpand
1<snip> 2 ALLEGRO_FILE *testfile; 3 char str[] = "File Open\n"; 4 char pathstr[100]; 5 char *pathptr; 6 int error; 7 8 al_init(); 9 10 al_set_standard_file_interface(); 11 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_DATA_PATH); 12 sprintf(pathstr,"%s",al_path_cstr(path, '/')); 13 al_change_directory(al_path_cstr(path, '/')); 14 pathptr = al_get_current_directory(); 15 16 testfile = al_fopen("ipl.txt","w"); 17 error = al_fputs(testfile, str); 18 error = al_fclose(testfile); 19<snip>

(Incidentally, I haven't used al_android_set_apk_file_interface() here, so that's not causing problems)

If I step through the code, I see:

pathstr = data/user/0/com.tootiredgames.gravstorm/files (output from get_standard_path)
pathptr = /data/data/com.tootiredgames.gravstorm/files (output from get_current_directory)

If I look at the filesystem, either with an android file manager, or under windows when connected by USB, I see a path that looks more like:

This PC\MotoE2(4G-LTE)\Internal storage\Android\data\com.tootiredgames.gravstorm\files

al_fopen returns a pointer (i.e. not NULL) and both al_fputs() and al_fclose() return success.

So, can anyone help with any of these questions:

What should I be doing differently to write a file, and where can I find it?

Failing answers to that, does anyone know:
- Why do I end up with different values for pathstr and pathptr?

- Why do neither of them match what I see with a file browser?

- Where, if anywhere, has my file been written?

Cheers,

Ian.

beoran

Why don't you try to open the file after writing closing it, and then read it again? What you see in Windows is deceiving, Windows mounts the file system in a different way than Android does. What you will get in your app will be different, especially the first few directories.

Ian Lewis

Good idea :) I tried that, and it seems that my app can happily read back what it's written, even though all the file browsers are conspiring to hide it from me ;)

In a practical sense, my problem is solved (thank you beoran) though I'm still interested if anyone can enlighten me as to how all these paths relate to each other, where my files are being written, and why I can't see them???

Cheers,

Ian.

beoran

Android is Linux and you can have several symbolic or hard links to a single file or directory. So, both pathstr and pathptr point to the same path, even though the names differ. You can use that directory without problems, and if you need the contents of that file, just send it over the network. You might need to use command line tools to find back the files "real" location.

Thread #617084. Printed from Allegro.cc