Advertisement
BigETI

[TUTORIAL] Read/Write binary files

Oct 23rd, 2012
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.69 KB | None | 0 0
  1. /*
  2.     Tutorial: How to create and read binary files using fgetchar and fputchar by Ethem Kurt :)
  3.    
  4.     Why binary files are good to use?
  5.     Binary files are used for computer storage, and for processing purpose. If you need from or store data to a plain text file, you have to convert it first to binary. Binary files usually doesn't need this step at all, which will save us processing time.
  6. */
  7.  
  8. //Step 1: Creating a binary file
  9. /*
  10.     Basicly you just store 8 bit numbers into a file.
  11.     I also prefer to use the macro below which will help you to write the script less longer.
  12. */
  13. #define fwritechar(%0)  fputchar(%0, false) //Usage: fwritechar(File:file_handle, value);
  14. #define freadchar(%0)   fgetchar(%0, 0, false) //Usage: freadchar(File:file_handle);
  15. // Rewrite/Create a file handle
  16. #define MY_FILE "myfile.bin" // <- Some definition of our file
  17. new File:myfile = fopen(MY_FILE, io_write); // <- This will rewrite or create this file.
  18. if(myfile) // <- If success
  19. {
  20.     //Let us store here a bunch of information with only numbers or characters.
  21.     fwritechar(myfile, 'H');
  22.     fwritechar(myfile, 'i');
  23.     fwritechar(myfile, '!');
  24.     fwritechar(myfile, 0x65);
  25.     fwritechar(myfile, 0x55);
  26.     fwritechar(myfile, 0);
  27.     fwritechar(myfile, 100);
  28.     fclose(myfile); // <- Saves and closes the file.
  29. }
  30. else printf("Couldn't open file \"%s\".", MY_FILE); // <- Else if it can't open the file handle due to a random error. (Happens rarely!)
  31.  
  32. //Advanced usage on 16, 24, 32 bit numbers
  33.  
  34. for(new i = 0; i < 2; i++) fwritechar(myfile, (1000>>>(i*8))&0xFF); //Stores a 16 bit number into a binary type of file
  35.  
  36. for(new i = 0; i < 3; i++) fwritechar(myfile, (0x154684>>>(i*8))&0xFF); //Stores a 24 bit number into a binary type of file
  37.  
  38. for(new i = 0; i < 4; i++) fwritechar(myfile, (0x15684626>>>(i*8))&0xFF); //Stores a 32 bit number into a binary type of file
  39.  
  40. //Store numbers with tags
  41.  
  42. for(new i = 0; i < 4; i++) fwritechar(myfile, (_:1456.041568>>>(i*8))&0xFF); //Removes tag of the Float tagged number and stores a 32 bit number (works with all tags aswell!)
  43.  
  44. // Example:
  45. new File:myfile = fopen("somefile.pos", io_write); // <- Declarate a File: variable
  46. if(myfile)
  47. {
  48.     new Float:pPos[3]; // <- Declarate a Float: array
  49.     GetPlayerPos(playerid, pPos[0], pPos[1], pPos[2]); // <- Get player position
  50.     for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position X
  51.     for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position Y
  52.     for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position Z
  53.     fclose(myfile);
  54. }
  55.  
  56. //Step 2: Reading from a binary type of file.
  57. /*
  58.     Yes it is very simple to read binary data from files.
  59.     To do it we use the native fgetchar() which is included inside our freadchar() macro.
  60. */
  61. // Open a file handle
  62. #define MY_FILE "myfile.bin" // <- Some definition of our file
  63. new File:myfile = fopen(MY_FILE, io_read); // <- This will open this file in read mode.
  64. if(myfile) // <- If success
  65. {
  66.     new buffer = -1; // <- Our buffer to get the data from the native fgetchar() and if it returns -1 (0xFFFFFFFF) so we reached the end of file.
  67.     while((buffer = freadchar(myfile)) != -1) // <- Loop through the whole binary type of file
  68.     {
  69.         printf("%d; %c; 0x%x", buffer, buffer, buffer); // <- Prints stuff from any file you read.
  70.     }
  71.     fclose(myfile);
  72. }
  73. else printf("Couldn't open file \"%s\".", MY_FILE); // <- Else if it can't open the file handle due to a random error. (Happens rarely!)
  74.  
  75. // Advanced usage on 16, 24 and 32 bit numbers
  76.  
  77. for(new i = 0; i < 2; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 16 bit number into the variable
  78.  
  79. for(new i = 0; i < 3; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 24 bit number into the variable
  80.  
  81. for(new i = 0; i < 4; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 32 bit number into the variable
  82.  
  83. //Store numbers with tags
  84. new Float:myvar;
  85. for(new i = 0; i < 4; i++) myvar |= Float:(freadchar(myfile)<<(i*8)); //Stores a 32 bit number with a Float tag (works with all tags aswell!)
  86.  
  87. // Example:
  88. new File:myfile = fopen("somefile.pos", io_read); // <- Declarate a File: variable
  89. if(myfile)
  90. {
  91.     new Float:pPos[3]; // <- Declarate a Float: array
  92.     for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position X
  93.     for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position Y
  94.     for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position Z
  95.     fclose(myfile);
  96.     SetPlayerPos(playerid, pPos[0], pPos[1], pPos[2]);
  97. }
  98.  
  99.  
  100. // Do you need help with binary files? There are plenty amount of hex editors in the internet for editing such files easily. Normal text editors may fail at such kind of files.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement