1
0
rwu_particles_informatikpro.../utils.c

34 lines
667 B
C
Raw Normal View History

2020-03-18 11:29:08 +00:00
#include <stdio.h>
#include <malloc.h>
2020-03-18 11:53:11 +00:00
#include <errno.h>
#include <string.h>
2020-03-18 11:29:08 +00:00
#include "utils.h"
#define BUFFER_SIZE 1024
2020-03-18 11:53:11 +00:00
char *readFile(char *filename)
2020-03-18 11:29:08 +00:00
{
2020-03-18 11:53:11 +00:00
FILE *file;
char *buffer;
long numbytes;
2020-03-18 11:29:08 +00:00
2020-03-18 11:53:11 +00:00
if((file = fopen(filename, "r")) == NULL)
2020-03-18 11:29:08 +00:00
{
2020-03-18 11:53:11 +00:00
printf("ERROR open file %s: %s\n", filename, strerror(errno));
}
fseek(file, 0L, SEEK_END);
numbytes = ftell(file);
fseek(file, 0L, SEEK_SET);
2020-03-18 11:29:08 +00:00
2020-03-18 11:53:11 +00:00
if((buffer = calloc(numbytes, sizeof(char))) == NULL)
{
printf("ERROR allocating memory: %s\n", strerror(errno));
2020-03-18 11:29:08 +00:00
}
2020-03-18 11:53:11 +00:00
fread(buffer, sizeof(char), numbytes, file);
2020-03-18 11:29:08 +00:00
fclose(file);
2020-03-18 11:53:11 +00:00
return buffer;
2020-03-18 11:29:08 +00:00
}