libdart_openmvg/src/util.cpp

31 lines
688 B
C++

#include "./util.h"
size_t read_file(const char *path, uint8_t **contents)
{
FILE *file = fopen(path, "rb");
if (file == NULL)
{
printf("Error: Unable to open file.\n");
return 0;
}
// Determine the file size
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
rewind(file);
// Allocate memory for the contents array
*contents = (uint8_t *)malloc(file_size);
if (*contents == NULL)
{
printf("Error: Unable to allocate memory.\n");
fclose(file);
return 0;
}
// Read the file contents into the array
fread(*contents, 1, file_size, file);
fclose(file);
return file_size;
}