Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the Windows C library from converting "\r\n" to "\n" when reading a text file in C?

Tags:

c

newline

windows

I have a normal text file that lines end with normal \r\n. However, when using 'open' and 'read', Windows convert all the \r\n to \n. I know this means I have to open the file in binary mode but the function 'open' doesn't give this option, it has only read-only, write-only, or read-write.

This is the code:

int File_Size = ...; 
char* Buffer = (char*)malloc(File_Size);

int Handle = open(File_Path,O_RDONLY);
read(Handle,Buffer,File_Size);
close(Handle);
like image 758
jondinham Avatar asked Nov 03 '11 12:11

jondinham


1 Answers

Try using O_RDONLY|O_BINARY.

like image 85
lhf Avatar answered Nov 18 '22 23:11

lhf