[a5] al_fopen trouble (using PhysFS)
SpectreNectar
ALLEGRO_FILE *file = al_fopen("level.txt", "wb");
    if(file==0) {


        std::cout << "fail" << std::endl;

    }

Hello, I don't get why this outputs fail :(
It should create a file for writing, right? Even if one doesn't exist?
So... what could possibly cause it to do that?

Edgar Reynaldo

If it is a text file, you shouldn't use "wb", but "w" for text mode.

As for why it fails - you might not have write permission to the directory it is trying to create a file in.

You said you were using PhysFS, so what are you actually trying to write to? A zip file?

Show more code.

SpectreNectar

This should be all the code relevant for physfs (if I remember correctly):

#SelectExpand
1bool setup( Assets& v ) { 2 3 al_set_physfs_file_interface(); 4 5 /* Set up PhysicsFS. */ 6 if(!PHYSFS_init(0)) std::cout << "failed initializing" << std::endl; 7 else if(!PHYSFS_addToSearchPath("data", 1)) std::cout << "failed adding search path" << std::endl; 8 9 ... 10} 11 12bool deinit( Assets& v ) { 13 14 PHYSFS_deinit(); 15 16 ... 17 18}

I can't find other places that messes with file interfaces by searching. There shouldn't be any either.
I'm attempting to write to an ordinary folder. Or... actually I'm not sure. I'm writing to the root directory and considering the nature of physfs that could be several places. I think.
I still don't know what to do though.
Here's what I'm trying to do:

#SelectExpand
1void eEditor::save() { 2 3 ALLEGRO_FILE *file = al_fopen("level.txt", "wb"); 4 if(file==0) { 5 6 7 std::cout << "fail" << std::endl; 8 9 } 10 11 std::stringstream ss; 12 13 ss << draw_list_len; 14 15 al_fputs(file, ss.str().c_str()); 16 //al_fputs(file, " "); 17 //al_fwrite(file, " ", strlen(" ")); 18 19 al_fclose(file); 20 21}

Edgar Reynaldo

Try calling PHYSFS_init before calling al_set_physfs_file_interface.

Make sure your current working directory is correct, and make sure that the 'data' folder exists.

SpectreNectar

I tried doing as you suggested but it was still the same result.

I think my current working directory is wrong like you suggest actually. But I'm not sure how to set it or figure out what it is. The data dir exists - I'm already loading resources in and they are in that dir. I actually copy pasted the path originally.

Trent Gamblin

Try al_set_standard_file_interface() before doing that.

Edgar Reynaldo

If you're using gcc, you can use getcwd(const char* buffer , int buf_size); to check the current working directory.

A5 has functions to change the current working directory and find the full path to your executable.
See
al_get_standard_path
and
al_change_directory

SpectreNectar

If I call al_set_standard_file_interface() just before al_fopen then it DOES work, but the program crashes because I need PhysFS to display the font and graphics of the closing dialog I use (and basically everything with resources everywhere).

My problem indeed seems to be figuring out how to open a file for writing using the physfs file interface.

So I'm stuck again with this and don't know where to go from here:

#SelectExpand
1void eEditor::save() { 2 3 if(!al_change_directory("/lvl")) std::cout << "no change of dir" << std::endl; //Outputs 4 5 ALLEGRO_FILE *file = al_fopen("level.bin", "wb"); 6 if(file==0) { 7 8 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 9 10 char * dir = al_get_current_directory(); 11 12 std::cout << "fail: " << al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP) << std::endl; //Outputs the directory of the executable 13 std::cout << "info: " << dir << std::endl; //Outputs / 14 15 al_free(dir); 16 17 al_destroy_path(path); 18 19 20 } 21 22 al_fclose(file); 23 24}

Also a small change here...

/* Set up PhysicsFS. */
    if(!PHYSFS_init(0)) std::cout << "failed initializing" << std::endl;
    else {

        al_set_standard_file_interface();
        al_set_physfs_file_interface();

        if(!PHYSFS_addToSearchPath("data", 1)) std::cout << "failed adding search path" << std::endl;
        if(!PHYSFS_addToSearchPath("lvl", 1)) std::cout << "failed adding search path2" << std::endl;
    }

EDIT:
DO I need to use this:
ALLEGRO_FILE *file = al_create_file_handle(al_get_new_file_interface(), 0);
?

Thomas Fjellstrom

I think you need to setup the main filesystem as a mount in PhysFS or you can't access the main filesystem. "Standard Path's" mean nothing in PhysFS if you don't have the filesystem "mounted".

Trent Gamblin

You can switch back to the physfs interface at any time with al_set_physfs_file_interface();

... that's probably not ideal, something like Thomas said might work better, but I just switch back and forth as needed.

SpectreNectar

Alright I'll switch back and forth from interfaces until I learn how setting up mounts work. It wont be much of a problem in this case really.

Thanks very much for your comments/advice.

Thread #608366. Printed from Allegro.cc