File_-_ReadDirIntoCharArray.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "raylib.h"
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <string.h>
  6. int main(void)
  7. {
  8. // Initialization
  9. //--------------------------------------------------------------------------------------
  10. const int screenWidth = 800;
  11. const int screenHeight = 450;
  12. InitWindow(screenWidth, screenHeight, "raylib example.");
  13. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  14. //--------------------------------------------------------------------------------------
  15. // Out char where we store the filenames.
  16. char files[10024][PATH_MAX];
  17. int numfiles=0;
  18. // Here we read the current dir(where this example is run)
  19. // and copy the filename into the files char array.
  20. DIR *d;
  21. struct dirent *dir;
  22. d = opendir(".");
  23. if (d)
  24. {
  25. while ((dir = readdir(d)) != NULL)
  26. {
  27. strcpy(files[numfiles],dir->d_name);
  28. numfiles++;
  29. }
  30. closedir(d);
  31. }
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. //----------------------------------------------------------------------------------
  38. // Draw
  39. //----------------------------------------------------------------------------------
  40. BeginDrawing();
  41. ClearBackground(RAYWHITE);
  42. DrawText(FormatText("Number of files in dir : %i",numfiles),10,10,20,BLACK);
  43. // Draw the first chunk of the dir to the screen.
  44. if(numfiles>0){
  45. int x=0;
  46. int y=20;
  47. for(int i=0;i<numfiles;i++){
  48. DrawText(FormatText("%s",files[i]),10+x,10+y,10,BLACK);
  49. y+=10;
  50. if(y>screenHeight-50){
  51. x+=200;
  52. y=20;
  53. }
  54. }
  55. }
  56. EndDrawing();
  57. //----------------------------------------------------------------------------------
  58. }
  59. // De-Initialization
  60. //--------------------------------------------------------------------------------------
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }