In the function below, I have a commented printf. When I uncomment it, the function works perfectly fine, when it remains commented, the function seems to access random memory or randomly cuts off at the 8th character, seemingly erratically. Why does this printf situation save the function? Other fixes or debug attempts I have tried have failed.
char* temp(char* file, int max) {
//First read the file and get info
FILE *r = fopen(file, "r");
char text[max];
char ch;
int i = 0;
while ((ch = fgetc(r)) != EOF){
text[i] = ch;
i++;
}
//printf("%d",i);
text[i] = '\0';
//close up
fclose(r);
char *rt = text;
return rt;
}
Suggestions?