[A5] al_filename_exists() can't see file.
Neil Roy

What I am doing is scanning a ZIP file which contains the games default levels and basically counting the number of levels available. Then I switch out of the ZIP to the exe directory and scan for any additional user made levels so I can get an accurate count of the total number of levels available.

There are 10 levels in the zip ("level001.dp2" to "level010.dp2") and 1 level outside the zip in the same directory as the exe named "level011.dp2". It finds the 10 inside the zip just fine, but doesn't see the one in the exe folder. The path functions work well and get the proper path but nothing. I switch off the physfs interface okay so I don't see where I am going wrong here.

#SelectExpand
1 // Get path 2 char path[TEXT_BUFFER]; 3 snprintf(path, TEXT_BUFFER, "%s", 4 al_path_cstr(al_get_standard_path(ALLEGRO_RESOURCES_PATH), 5 ALLEGRO_NATIVE_PATH_SEP)); 6 7 int levelcount = 0; 8 // Get number of levels in the zip 9 while(1) { 10 char filename[TEXT_BUFFER]; 11 snprintf(filename, TEXT_BUFFER, "level%03d.dp2", levelcount+1); 12 if(al_filename_exists(filename)) levelcount++; 13 else break; 14 } 15 16 al_set_standard_file_interface(); 17 // Get the number of levels inside the exe folder 18 while(1) { 19 char filename[TEXT_BUFFER]; 20 snprintf(filename, TEXT_BUFFER, "%slevel%03d.dp2", path, levelcount+1); 21 printf("Searching for %s\n", filename); 22 if(al_filename_exists(filename)) levelcount++; 23 else break; 24 } 25 printf("Levels found: %d\n", levelcount); 26 al_set_physfs_file_interface();

The printf() shows the path and filename as being totally correct. Also, just a little bit later in my code I load in levels for the game and they all load just fine, whether they are in the ZIP or in the main directory with the exe, no problems, they load fine. I do the same thing in this code where I switch to the standard file interface and then open the file. It works, no problem. Yet in the above code, it won't file the level outside the zip no matter what I do. I tried al_fopen() as well and nothing. This is really strange and starting to drive me nuts! :/

Thomas Fjellstrom

You'll want to use absolute paths with those functions.

Personally I'd just al_read_directory the dir your levels will be in, and look for level\d+.dp2 files.

Neil Roy

Thanks.

I replaced the al_filename_exists() with the following code and it is working now...

      ALLEGRO_FILE *file = NULL;
      file = al_fopen(levelname, "rb");
      if(file) {
         levelcount++;
         al_fclose(file);
      }

...which ironically, was the way I was originally going to do it until I made the mistake of discovering al_filename_exists() ;)

Thomas Fjellstrom

To be honest, al_filename_exists should work. I suspect you weren't building the path right.

Next time you want to play with paths, ALLEGRO_PATH might be a good way to go.

Neil Roy

To be honest, al_filename_exists should work. I suspect you weren't building the path right.

I used the same path for al_fopen() that I used for al_filename_exists(), I only changed the code that opens it.

The old code is above, here's the new code with changes:

#SelectExpand
1 char path[TEXT_BUFFER]; 2 snprintf(path, TEXT_BUFFER, "%s", al_path_cstr(al_get_standard_path(ALLEGRO_RESOURCES_PATH), 3 ALLEGRO_NATIVE_PATH_SEP)); 4 5 int levelcount = 0; 6 while(1) { 7 snprintf(levelname, TEXT_BUFFER, "level%03d.dp2", levelcount+1); 8 ALLEGRO_FILE *file = NULL; 9 file = al_fopen(levelname, "rb"); 10 if(file) { 11 levelcount++; 12 al_fclose(file); 13 } 14 else break; 15 } 16 17 al_set_standard_file_interface(); 18 while(1) { 19 snprintf(levelname, TEXT_BUFFER, "level%03d.dp2", levelcount+1); 20 ALLEGRO_FILE *file = NULL; 21 file = al_fopen(levelname, "rb"); 22 if(file) { 23 levelcount++; 24 al_fclose(file); 25 } 26 else break; 27 } 28 printf("Levels found: %d\n", levelcount); 29 al_set_physfs_file_interface();

One thing I changed with regard to the path was I used levelname which was a variable I already had created so I didn't need a filename variable, but that didn't effect anything, and I tried the code with it and al_filename_exists() and still no go. The path is built the same way, I thought of that too. I built it several different ways, which is why I needed to know how to get the directory path that the executable was in (in another thread) and that didn't help either. Speaking of which, I just realized that code is still in there and no longer needed. It wasn't until I changed it to al_fopen() (originally tried fopen() and that worked, then the Allegro versions). So there seems to be some sort of bug with al_filename_exists(). There's absolutely nothing I done wrong, it's not a complicated task, my game builds the file name and loads the levels all the time, they have for over a year now, it's a straight forward task. I have a level editor that I worked on long before this and of course I have worked with C programming and files since the 1990s. I have trouble the odd time with Allegro 5, but not with C and files. :)

One bonus to all this, the new C standard C2011, adds a new standard subspecifier ("x"), that can be appended to any "w" specifier (to form "wx", "wbx", "w+x" or "w+bx"/"wb+x"). This subspecifier forces the function to fail if the file exists, instead of overwriting it. Could be handy, I may update my level editor to use it.

Thomas Fjellstrom

I don't suppose you could print out the actual filenames you're trying to al_fopen, and al_filename_exists? I half expect you're missing a slash in the latter, or some similar issue.

Neil Roy

In both cases I initially just used "level011.dp2" as my filename, no path, which works fine with al_fopen but not with al_filename_exists. I added in a path later on in an effort to get it to work but it wouldn't. al_fopen has zero problems opening the level files and reading in all the data.

Strange anyhow. Would look more elegant with just al_filename_exists but it's not vitally important for me. If it is a problem with the library it should be looked into though.

Thomas Fjellstrom

it's probably a bug then...

Neil Roy

Okay, this is strange... but I just went back on a whim, to rewrite the code again and clean it up and I decided to try out al_filename_exists again. And it worked. Which is really strange as the filename hasn't changed at all. I am totally stumped as to why this would work now, and not last night.

#SelectExpand
1 int levelcount = 0; 2 bool file_found; 3 do { 4 snprintf(levelname, TEXT_BUFFER, "level%03d.dp2", levelcount+1); 5 file_found = al_filename_exists(levelname); 6 if(file_found) levelcount++; 7 } while(file_found); 8 9 al_set_standard_file_interface(); 10 do { 11 snprintf(levelname, TEXT_BUFFER, "level%03d.dp2", levelcount+1); 12 file_found = al_filename_exists(levelname); 13 if(file_found) levelcount++; 14 } while(file_found); 15 al_set_physfs_file_interface(); 16 17 printf("Levels found: %d\n", levelcount);

I don't know, must have been something I done I guess, but I can't see what.

Winfield

Maybe last night you were running your application from the wrong working directory?

Thomas Fjellstrom

The main difference I see is that you were prepending the path variable to the levelname. If levelname is missing a trailing slash on path, it'd fail to find the file.

Which by the way, is why I wanted you to print out (and post) the paths it was trying to test.

Neil Roy
Winfield said:

Maybe last night you were running your application from the wrong working directory?

I always run it in the same directory.

The main difference I see is that you were prepending the path variable to the levelname. If levelname is missing a trailing slash on path, it'd fail to find the file.Which by the way, is why I wanted you to print out (and post) the paths it was trying to test.

This was not the problem as I used printf to show me the path+filename it was searching for in the console window. Plus it also happened when I didn't use a path at all (which is what it does now), the reason why I added the path in there in the first place was because it wasn't working. Just one of many attempts to get it working. Strange problem.

I thought for a while it could have been because I had temporarily renamed the file to "level001.dp2" in order to test if the game would load it in rather than the default from the zip file, and the game loaded it no problem so it could see it in the directory with the game. I renamed it back to what it was then just tried al_fopen and it worked. When I switched back to try al_filename_exists one last time (because I am stubborn like that, ;D) it suddenly was working. shrug ???

Thomas Fjellstrom

Ok, I suspect typo now ;)

Neil Roy
Quote:

Ok, I suspect typo now ;)

You're probably right. It was late. But I honestly went over it quite a few times and didn't see any. I DID play around with other functions which I suspect may have effected it somewhat. I know I used the wrong functions a few times while trying to figure it out. Ah well, it works now. :)

Love Allegro 5 for the most part (though I still don't like how the audio is implemented, but that's another subject).

Anyhow, I should probably close this topic. :D

Thread #613220. Printed from Allegro.cc