ReadCompleteFile
- Function
- August 22, 2025
Table of Contents
ReadCompleteFile
ReadCompleteFile
Description
Read complete contents of file at once. Pointer returned is malloc’d and hence must be freed after use. The returned pointer can also be reused by providing pointer to it in data
parameter. realloc
is called on *data
in order to expand it’s size. If *capacity
exceeds the size of file to be loaded, then no reallocation is performed. This means the provided buffer will automatically be expanded if required. The returned buffer is null-terminated just-in-case. The implementation and API is designed in such a way that it can be used with containers like Vec and Str.
Parameters
Name | Direction | Description |
---|---|---|
filename | in | Name/path of file to be read. |
data | in,out | Memory buffer where loaded file will be stored. |
file_size | out | Complete size of file in bytes will be stored here. |
capacity | in,out | Hints towards current capacity of data buffer. New capacity of data buffer is automatically stored here if realloc is performed. |
Success
true
Failure
false
Usage example (Cross-references)
- In
File.c:15
:
#include <Misra/Types.h>
bool ReadCompleteFile(const char *filename, char **data, size *file_size, size *capacity) {
if (!filename || !data || !file_size || !capacity) {
LOG_ERROR("invalid arguments.");
- In
MisraEnum.c:50
:
Str code = StrInit();
ReadCompleteFile(input_filename, &code.data, &code.length, &code.capacity);
EnumEntries entries = VecInit();
- In
MisraDoc.c:112
:
Str config = StrInit();
Scope(&config, StrDeinit, {
if (!ReadCompleteFile(config_path, &config.data, &config.length, &config.capacity)) {
LOG_FATAL("Failed to read config file.");
}
- In
MisraDoc.c:167
:
Str file_contents = StrInit();
Scope(&file_contents, StrDeinit, {
if (!ReadCompleteFile(
file_path.data,
&file_contents.data,
- In
Main.c:5
:
int main(void) {
Str file = StrInit();
if (ReadCompleteFile("Bin/Demangler/CppNameManglingGrammar", &file.data, &file.length, &file.capacity)) {
Strs lines = StrSplit(&file, "\n");