fread dropping carriage returns in C?

女生的网名这么多〃 提交于 2019-12-01 20:11:35

You are seeing the effects of "text mode" where line endings are being converted from Windows to Unix aka C newlines.

You need to open the file in binary mode which means adding a "b" to the fopen flags. It would look like textFile = fopen("./Test.txt", "rb+")

Also, I am not sure I understand why you get the length of the file before reading it.

Just read it. Look at the return value of fread to see how much you read. If you use a size of 1 the return value will be the number of bytes read.

Like this:

fileSize = fread(fileCharacterBuffer, 1, sizeof(fileCharacterBuffer), textFile);

Open the file in binary format textFile = fopen("./Test.txt", "rb+");. Currently you are reading in text format and \r\n gets converted appropriately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!