Nuppiz

filetest.c

Oct 29th, 2021 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. char* filename;
  8.  
  9. void load_data(char* levelname)
  10. {
  11.     FILE* level_file;
  12.     char buffer[100];
  13.     char c;
  14.     int test_num;
  15.     int player_y;
  16.     int player_x;
  17.    
  18.     level_file = fopen(levelname, "r");
  19.    
  20.     if (level_file == NULL)
  21.     {
  22.         printf("Unable to open file: %s\n", levelname);
  23.         printf("Please check the file actually exists\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.    
  27.     while ((c = fgetc(level_file)) != EOF)
  28.     {
  29.         if (c == '$')
  30.         {
  31.             fscanf(level_file, "%s", buffer);
  32.             if (strcmp(buffer, "test") == 0)
  33.             {
  34.                 fscanf(level_file, "%d", &test_num);
  35.                 printf("Test number loaded, it is: %d\n", test_num);
  36.             }
  37.             else if (strcmp(buffer, "playerloc") == 0)
  38.             {
  39.                 fscanf(level_file, "%d", &player_y);
  40.                 fscanf(level_file, "%d", &player_x);
  41.                 printf("Player starts at Y: %d and X: %d\n", player_y, player_x);
  42.             }
  43.             else if (strcmp(buffer, "mapdata") == 0)
  44.             {
  45.                 while ((c = fgetc(level_file)) != EOF)
  46.                 {
  47.                     printf("%c", c);
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     fclose(level_file);
  54. }
  55.  
  56. void main()
  57. {
  58.     filename = malloc(12);
  59.    
  60.     printf("Enter file name:\n");
  61.     scanf("%s", filename);
  62.     printf("\n");
  63.     load_data(filename);
  64. }
  65.  
  66. $test
  67. 13
  68.  
  69. $playerloc
  70. 6 4
  71.  
  72. $mapdata
  73. WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
  74. W*W-^-W--------------------------------W
  75. W-W---W--------------------------------W
  76. W-WWW----------------------------------W
  77. W------------------------------------WWW
  78. W^W-W^^-^------------------------------W
  79. W------------------------------------WWW
  80. W-WWW--------------------------------|-W
  81. W--^---------------------------------WeW
  82. WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Add Comment
Please, Sign In to add comment