Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Tutorial: How to create and read binary files using fgetchar and fputchar by Ethem Kurt :)
- Why binary files are good to use?
- 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.
- */
- //Step 1: Creating a binary file
- /*
- Basicly you just store 8 bit numbers into a file.
- I also prefer to use the macro below which will help you to write the script less longer.
- */
- #define fwritechar(%0) fputchar(%0, false) //Usage: fwritechar(File:file_handle, value);
- #define freadchar(%0) fgetchar(%0, 0, false) //Usage: freadchar(File:file_handle);
- // Rewrite/Create a file handle
- #define MY_FILE "myfile.bin" // <- Some definition of our file
- new File:myfile = fopen(MY_FILE, io_write); // <- This will rewrite or create this file.
- if(myfile) // <- If success
- {
- //Let us store here a bunch of information with only numbers or characters.
- fwritechar(myfile, 'H');
- fwritechar(myfile, 'i');
- fwritechar(myfile, '!');
- fwritechar(myfile, 0x65);
- fwritechar(myfile, 0x55);
- fwritechar(myfile, 0);
- fwritechar(myfile, 100);
- fclose(myfile); // <- Saves and closes the file.
- }
- 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!)
- //Advanced usage on 16, 24, 32 bit numbers
- for(new i = 0; i < 2; i++) fwritechar(myfile, (1000>>>(i*8))&0xFF); //Stores a 16 bit number into a binary type of file
- for(new i = 0; i < 3; i++) fwritechar(myfile, (0x154684>>>(i*8))&0xFF); //Stores a 24 bit number into a binary type of file
- for(new i = 0; i < 4; i++) fwritechar(myfile, (0x15684626>>>(i*8))&0xFF); //Stores a 32 bit number into a binary type of file
- //Store numbers with tags
- 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!)
- // Example:
- new File:myfile = fopen("somefile.pos", io_write); // <- Declarate a File: variable
- if(myfile)
- {
- new Float:pPos[3]; // <- Declarate a Float: array
- GetPlayerPos(playerid, pPos[0], pPos[1], pPos[2]); // <- Get player position
- for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position X
- for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position Y
- for(new i = 0; i < 4; i++) fwritechar(myfile, (_:pPos[0]>>>(i*8))&0xFF); // <- Store position Z
- fclose(myfile);
- }
- //Step 2: Reading from a binary type of file.
- /*
- Yes it is very simple to read binary data from files.
- To do it we use the native fgetchar() which is included inside our freadchar() macro.
- */
- // Open a file handle
- #define MY_FILE "myfile.bin" // <- Some definition of our file
- new File:myfile = fopen(MY_FILE, io_read); // <- This will open this file in read mode.
- if(myfile) // <- If success
- {
- 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.
- while((buffer = freadchar(myfile)) != -1) // <- Loop through the whole binary type of file
- {
- printf("%d; %c; 0x%x", buffer, buffer, buffer); // <- Prints stuff from any file you read.
- }
- fclose(myfile);
- }
- 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!)
- // Advanced usage on 16, 24 and 32 bit numbers
- for(new i = 0; i < 2; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 16 bit number into the variable
- for(new i = 0; i < 3; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 24 bit number into the variable
- for(new i = 0; i < 4; i++) myvar |= (freadchar(myfile)<<(i*8)); //Stores a 32 bit number into the variable
- //Store numbers with tags
- new Float:myvar;
- 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!)
- // Example:
- new File:myfile = fopen("somefile.pos", io_read); // <- Declarate a File: variable
- if(myfile)
- {
- new Float:pPos[3]; // <- Declarate a Float: array
- for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position X
- for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position Y
- for(new i = 0; i < 4; i++) pPos |= Float:(freadchar(myfile)<<(i*8)); // <- Reads position Z
- fclose(myfile);
- SetPlayerPos(playerid, pPos[0], pPos[1], pPos[2]);
- }
- // 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