initial commit

This commit is contained in:
wt committed 2025-09-09 13:19:59 +07:00
commit 9246e48f03
39 files changed
+34142

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
#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;
}
printf("%s\n%s\n", data, path);
if (fprintf(fp, "%s", data) < 0) {
perror("Error write file");
return nullptr;
}
fclose(fp);
char *ret = strdup(data);
return ret;
}