I have written a function which reads a file full of text, and proceeds to concatenate all the lines into a single string. It works, but the fclose() instruction, when used, launches an error :
"* Error in `./main': double free or corruption (out): 0x00000000020fc330 *" followed by a backtrace and a memory map.
It also works badly for files with a single line.
What can I do ?
The code :
char* readFile(char* fileName){
FILE* myFile = fopen(fileName, "r");
if (myFile != NULL) {
fseek(myFile, 0, SEEK_END);
long l = ftell(myFile);
fseek(myFile, 0, SEEK_SET);
char* content = (char*)malloc((size_t)l * sizeof(char));
if (content == NULL) return NULL;
char * chain = (char*)malloc((size_t)l * sizeof(char));
while(fscanf(myFile, "%s\n", chain) != EOF || fscanf(myFile, "%s\t", chain) != EOF || fscanf(myFile, "%s ", chain) != EOF){
strcat(content, chain);
}
free(chain);
fclose(myFile);
return content;
} else return NULL;
}