How to read Linux or Mac created file in Windows via C FILE *?

Since all we know in Windows EOL is CRLF and linux LF and CR on Mac. ( more_info )

I want to write a program that reads both linux and Mac, as well as Win files line by line in Windows. To open the file, I will use fopen in "rt" mode, but I do not know how to read lines. fgets reads before CRLF and LF on Windows, but I want it to work for EOL = CR files too.

So what is the solution? Thanks in advance.

+6
c ++ c file newline
source share
3 answers

Therefore, I could not find anything good for me and decided to write my own function my_fgets , which uses fgetc in the body.

+1
source share

To open in "t" mode, the file must correspond to the platform on which you work. Otherwise, you just need to open in binary mode and split the difference yourself.

The C library helps you write a program that works approximately the same on different platforms, but does not support cross-reading files.

+2
source share

While you open the file in rt (read-text) mode, you should be fine. fgets () will return a single line and handle subtle differences between platforms. When writing to a file, use "\ n" as the EOL, and you will get the correct line endings for your platform. This is the reason for opening the file in text mode. If you use binary mode, you will have to process all the different lines in your software.

+1
source share

All Articles