Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 5s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 7s
54 lines
1.1 KiB
C
54 lines
1.1 KiB
C
#include "file.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
char *read_file(const char *path) {
|
|
FILE *fp = fopen(path, "rb");
|
|
if (fp == NULL) {
|
|
printf("%s\n", path);
|
|
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;
|
|
}
|
|
|
|
char *write_file(const char *path, const char *data) {
|
|
FILE *fp = fopen(path, "w");
|
|
if (fp == NULL) {
|
|
printf("%s\n", path);
|
|
perror("Error opening file");
|
|
return nullptr;
|
|
}
|
|
if (fprintf(fp, "%s", data) < 0) {
|
|
perror("Error write file");
|
|
return nullptr;
|
|
}
|
|
fclose(fp);
|
|
char *ret = strdup(data);
|
|
return ret;
|
|
}
|