I am after a simple task: reading one line at a time from a file, printing the line and appending all the content in a char array. It all started with a Segmentation fault (core dumped)
from my project, I then kept on isolating my code until I reached this:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *fp;
fp = fopen("read.txt","r");
char buffer[255];
char longBuff[1024] = "";
while(fgets(buffer, 255, fp)) {
printf("%s\n",buffer);
strcat(longBuff, buffer);
}
fclose(fp);
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWTF%s\n", longBuff);
}
The read.txt
file:
short
this is Longer
+++++
sad
And the Output:
sad++is Longer
sad++is LongerWWWWWWWWWWWWWWWWWWWTFshort
When I was confidently expecting:
short
this is Longer
+++++
sad
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWTFshortthis is Longer+++++sad
I have been over multiple similar questions and most answers refer to carriage return but I still don't understand this behavior and what is the cause for it.