Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LOAD_TEXT_H
- #define LOAD_TEXT_H
- #include "stdio.h"
- #include "stdlib.h"
- char* TextFromFile(const char* fileName, size_t *sizeOut){
- FILE *file;
- /* Open the file */
- file = fopen(fileName, "r");
- if (file == NULL) {
- printf("Cannot open file %s\n", fileName);
- return NULL;
- }
- /* Get size of file for memory allocation */
- fseek(file, 0, SEEK_END);
- size_t size = ftell(file);
- fseek(file, 0, SEEK_SET);
- /* Allocate memory to the array */
- char *textArray = (char*)malloc( (size+1) );
- /* Store the information into the array */
- size_t read_count = fread(textArray, 1, size, file);
- fclose(file);
- textArray[read_count] = '\0';
- if (sizeOut != NULL){*sizeOut = size;}
- return textArray;
- }
- #endif //LOAD_TEXT_H
Add Comment
Please, Sign In to add comment