LibFile provides easy-to-use helper functions for file operations. This library simplifies the process of reading and writing files in C by offering straightforward functions to write data to a file and read the entire contents of a file into a string.
- Write Data to a File: Easily write data to a specified file.
- Read Entire File: Read the entire contents of a file into a dynamically allocated string, handling all memory management for you.
- Error Handling: Built-in error handling to manage common file operation issues.
Ensure you have a C compiler installed on your system, such as gcc or clang.
To start using LibFile, clone the repository, include it in your project and refer to the documentation for
guidance on how to implement each function within file operations.
To make sure you obtain the most updated stable version of shared library(a DLL in Windows or .so in Unix/Linux), you’ll download this software from its official Github repository.
First, confirm the latest version available in
their releases page. At the time of this writing,
the most current stable version is 1.0.1.
-
The following command will download the
1.0.1release and save the shared library file at /usr/local/lib/libfile.so, which will make this library globally accessible to clients:sudo curl -L "https://github.com/adrian-tech-enthusiast/libfile/releases/download/v1.0.1/libfile.so" -o /usr/local/lib/libfile.so -
Next, We need to tell the loader it is available for use, so let us update the cache, invoke the
ldconfigutility, which configures the system's dynamic loader. This configuration ensures that the loader will find the newly published library:sudo ldconfig;That should create a link to our shared library and update the cache so it is available for immediate use. To verify you can run:
ldconfig -p | grep file -
Copy the library header file(
filehelper.h) to where it can be included in one of the C compiler's search path. Typical locations are/usr/includeand/usr/local/include;sudo curl -L "https://raw.githubusercontent.com/adrian-tech-enthusiast/libfile/v1.0.1/include/filehelper.h" -o /usr/local/include/filehelper.h;
This allows the library header file to be included in your C project as follows:
#include <stdio.h> /* standard input/output functions */ #include <filehelper.h> /* C File Helper Library functions */
Without this
#include, the compiler would complain about missing declarations for functions provided by the C File Library. -
Finally, in order to compile your code with the library, add the link flag to the library during the compilation of your C project:
gcc -o my_program my_program.c -lfile
#include <filehelper.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *data = "Hello, World!";
const char *file_path = "example.txt";
// Write data to file
if (file_put_contents(file_path, data) != WRITE_SUCCESS) {
fprintf(stderr, "Failed to write to file.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}#include <filehelper.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *file_path = "example.txt";
char *file_contents = file_get_contents(file_path);
if (file_contents == NULL) {
fprintf(stderr, "Failed to read from file.\n");
return EXIT_FAILURE;
}
printf("File contents: %s\n", file_contents);
free(file_contents);
return EXIT_SUCCESS;
}To compile your program, you might use a command like this, depending on your setup:
gcc -o my_program my_program.c -lfilehelperReplace my_program.c with the name of your source file. Make sure that the libfile is correctly linked.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This library is licensed under the GNU General Public License v3.0 License. See the LICENSE file for details.
Mr. Adrián Morelos - adrian.tech.enthusiast@gmail.com