Strcat is not working. I am trying to read text from 2 files with a dynamic memory function. I could not understand where is the error.
This is my code:
#include <stdlib.h>
#include <stdio.h>
void fileTest (char* filename, char c[]){
char* s = (char*) malloc(sizeof(char) * 1000);
FILE *fp =fopen(filename, "r");
if (fp == NULL){
exit(-1);
}
while( !feof(fp)){
fgets(s,100,fp);
strcat(c,s);
}
free(s);
fclose(fp);
}
int main() {
char firstS []="";
char secondS []="";
fileTest("file.txt", firstS);
fileTest("file2.txt", secondS);
printf("Fist file: %s \n", firstS);
printf("Second file: %s", secondS);
return 0;
}
In file.txt writes "Hello", in file2.txt writes "World" and I am getting this output : Error Image How can I fix this code?