Difference between files written in binary and text mode

前端 未结 7 1805
南旧
南旧 2020-11-22 14:20

What translation occurs when writing to a file that was opened in text mode that does not occur in binary mode? Specifically in MS Visual C.

unsigned char bu         


        
7条回答
  •  抹茶落季
    2020-11-22 14:46

    We had an interesting problem with opening files in text mode where the files had a mixture of line ending characters:

    1\n\r
    2\n\r
    3\n
    4\n\r
    5\n\r
    

    Our requirement is that we can store our current position in the file (we used fgetpos), close the file and then later to reopen the file and seek to that position (we used fsetpos).

    However, where a file has mixtures of line endings then this process failed to seek to the actual same position. In our case (our tool parses C++), we were re-reading parts of the file we'd already seen.

    Go with binary - then you can control exactly what is read and written from the file.

提交回复
热议问题