binarylib.h
is a C library designed to simplify operations on binary files, including writing, appending, and reading binary data. The library provides a set of macros and functions to handle various file I/O tasks with minimal code.
- Writing Binary Files: Easily write buffers of data to binary files.
- Appending to Binary Files: Append data to existing binary files.
- Reading Binary Files: Read binary data from files using specific addresses or return data by value.
- Macros for convenience: Includes macros to streamline file I/O operations.
-
Clone the repository:
git clone https://github.com/your-username/binarylib.git
-
Include the header file in your project:
#include "binarylib.h"
-
You can install it by downloading the repository.
binaryWrite("data.bin", int, 42, sizeof(int));
This writes the integer 42
to data.bin
.
Alternatively, use the function version:
int value = 42;
binaryWriteRef("data.bin", &value, sizeof(int));
binaryAppend("data.bin", int, 100, sizeof(int));
This appends the integer 100
to data.bin
.
Alternatively, use the function version:
int value = 100;
binaryAppendRef("data.bin", &value, sizeof(int));
int data;
binaryRead("data.bin", 0, &data, sizeof(int));
printf("Data: %d\n", data);
int* data = (int*)binaryReadByValue("data.bin", 0, sizeof(int));
printf("Data: %d\n", *data);
free(data); // Don't forget to free the allocated memory
binaryWrite(_Filename, _Type, _Buffer, _ElementSize)
void binaryWriteRef(const char* _Filename, const void* _Buffer, size_t _ElementSize)
binaryAppend(_Filename, _Type, _Buffer, _ElementSize)
void binaryAppendRef(const char* _Filename, const void* _Buffer, size_t _ElementSize)
void binaryRead(const char* _FileName, unsigned long _Address, void* _Location, size_t _ElementSize)
void* binaryReadByValue(const char* _FileName, unsigned long _Address, size_t _ElementSize)
The library provides basic error handling for file operations and memory allocation. If an operation fails, the program will print an error message and terminate with EXIT_FAILURE
.
Feel free to contribute to this project by opening issues or submitting pull requests.