added database
This commit is contained in:
1 parent
c4ea7a4293
commit
4f67cf11bd
30 files changed
+601
-267
No files matched your search
+34
@@ -0,0 +1,34 @@
|
||||
#include "file.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *read_file(const char *path) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
perror("Error opening file");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
long file_size = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
char *buffer = (char *) malloc(file_size + 1);
|
||||
if (!buffer) {
|
||||
perror("Error allocating memory");
|
||||
fclose(fp);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread(buffer, 1, file_size, fp);
|
||||
if (bytes_read != file_size) {
|
||||
perror("Error reading file");
|
||||
free(buffer);
|
||||
fclose(fp);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
buffer[file_size] = '\0';
|
||||
fclose(fp);
|
||||
return buffer;
|
||||
}
|
||||
Reference in new issue
Block a user