file io - Why does fread() in c read extra '#newlines' characters? -


while trying copy file string using fread() ,i getting characters file equal number of new lines. here 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 program is:

1   2   <newline> 

link input : https://paste.fedoraproject.org/388281/46780193/
output printing ascii values of characters read:

6   49 10 50 10 13 10   

if input is:

1   2   3   <newline>   

link input: https://paste.fedoraproject.org/388280/
output is:

9   49 10 50 10 51 10 51 13 10   

i saw other test cases.in every test case number of characters number of new lines.
have few questions:
-why pattern this?
-how this related fact new line take 2 bytes in windows?
-how rid of characters?
googled similar questions ,but didn't find answer.please explain?

calling ftell on stream opened in text mode, such in example not meaningful1.

the usage of function fread not correct, size , count arguments switched. means read partial, since file doesn't have 5000000 characters in them. values of elements in array after call, have indeterminate2 values. (the logical element in case being single element of size 5000000.)

the results you're seeing aren't meaningful. reading indeterminate values can cause undefined behavior.

the correct way read file pass correct parameters fread , use return value determine number of read characters:

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <assert.h>  int main() {     unsigned char in[500] = { 0 } ;     file *f=fopen("in.txt","r");     assert( f ) ;      const size_t read = fread(in,1,500,f);     printf( "read: %zu\n" , read );      for( size_t index = 0 ; index < read ; index++ )     {         printf( "%hhu " , in[index] );     }      fclose( f ); } 

using correct program, when file has content (dots aren't part of file):

. 1 2 3  . 

will read , print correct values:

read: 7 49 10 50 10 51 10 10 

one newline character, represented3 value 10, each number, , additional 1 @ end.


1 (quoted from: iso:iec 9899:201x 7.21.9.4 ftell function 2)
text stream, file position indicator contains unspecified information, usable fseek function returning file position indicator stream position @ time of ftell call; difference between 2 such return values not meaningful measure of number of characters written or read.

2 (quoted from: iso:iec 9899:201x 7.21.8.1 fread function 2)
if partial element read, value indeterminate.

3 in windows files, newline represented 2 characters: 13, 10. carriage return , line feed. when reading file in text mode, newline line feed character: 10. saw character 13 because of behavior of program wasn't meaningful. if (correctly) opened , read file in binary mode, see newline represented both characters.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -