I have this code where I want to read from a text file each line and then print it to the standard output. For no reason it only saves the last line of the file in that array of chars.
Any tips why(some explanations are)?
9 int main(){
10 FILE * f;
11 char buffer[255];
12 char * arr[255];
13 int i=0,n;
14
15 f = fopen("input.txt", "r");
16 while(1){
17 if(fgets(buffer,255,f) != NULL ){
18 arr[i++] = buffer;
19 }else break;
20 }
21 n=i;
22 for(i=0;i<n;i++){
23 printf("%s",arr[i]);
24 }
25
26 fclose(f);
27 return 0;
28 }
Your program only has one one buffer that gets overwritten with each line of the file
EDIT: