I wonder if such style of coding is okay in c and how to do that,
let me explain I want load disk file into ram with just one function
call of my production
I defined
struct bytes
{
char* begin;
char* end; //end is a first char after the data
}
the function mallocs appriopriate ram ant then reads whole file into
it and thats all
int get_file_size(FILE* file)
{
int seek_pos = ftell(file);
fseek(file, 0, SEEK_END);
int file_length = ftell(file) ;
fseek(file, seek_pos, SEEK_SET);
return file_length;
}
bytes read_file_and_alloc_bytes(char* file_name)
{
FILE * file = fopen(file_name, "rb");
if(file == NULL) ERROR_EXIT("\n input file %s not found", file_name);
int file_length = get_file_size(file);
char* data = malloc(file_length);
if(data == NULL) ERROR_EXiT("\n could not malloc data for file");
int elements_read = fread(data, 1, file_length, file);
int error = fclose(file);
if(error) ERROR_EXIT("error closing file %s", file_name);
bytes loaded_bytes = {data, data + file_length};
return loaded_bytes;
}