BigETI

Load data from file tutorial

Apr 13th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 1.08 KB | None | 0 0
  1. //Reading data from file
  2.  
  3. #define INFO_FILE   "savefile.txt" // Definition of our info file
  4. new info[3], File:myfile; // Our info array and file variable
  5. if(fexist(INFO_FILE)) // if the file exist
  6. {
  7.     myfile = fopen(INFO_FILE, io_read); // Let us read from this file
  8.     if(myfile) // If the file has been opened properly
  9.     {
  10.         new buffer[128], bufferlen; // Buffer array (string) and the lenght of the buffer after getting a line from this file
  11.         while(fread(myfile, buffer)) // Iteration methode of reading the whole file
  12.         {
  13.             bufferlen = strlen(buffer); //It's not good using strlen(buffer) inside the loop so..
  14.             for(new i = buferlen-1; i >= 0; i--) if(buffer[i] == '\r' || buffer[i] == '\n') buffer[i] = 0; // Iteration methode removing the next line character for later usage.
  15.             info[i] = strval(buffer); // Storing the value inside an array
  16.             printf("Information %d: Value %d", i+1, info[i]); // Printing the values (debugging)
  17.         }
  18.     }
  19.     else printf("Error at reading the file '%s'", INFO_FILE); // If an error occurs
  20. }
  21. else printf("File '%s' doesn't exist.", INFO_FILE); // Otherwise...
Advertisement
Add Comment
Please, Sign In to add comment