while I am trying to copy a file into string using fread() ,I am getting extra characters from file which is exactly equal to number of new lines. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define LEN 5000000
int main()
{
char *in = (char*) malloc(LEN);
FILE *f=fopen("in.txt","r");
fread(in,5000000,1,f);
printf("%ld\n", ftell(f));
in[ftell(f)]=0;
int l;
for(l=0;true;l++)
{
if(in[l]<10)
break;
printf("%d ",in[l]);
}
printf("\n");
}
Input for this program is:
1
2
<newline>
link for input : https://paste.fedoraproject.org/388281/46780193/
For output i am printing ASCII values of characters read:
6
49 10 50 10 13 10
if Input is:
1
2
3
<newline>
link for input: https://paste.fedoraproject.org/388280/
then output is:
9
49 10 50 10 51 10 51 13 10
I saw some other test cases.In every test case extra number of characters are always number of new lines.
I have few questions:
-why the pattern is like this?
-How is this this related to the fact that new line take 2 bytes in windows?
-How to get rid of those extra characters?
I googled for similar questions ,but didn't find answer.Please somebody explain?