how to write an integer to a file (the difference between fprintf and fwrite)

人盡茶涼 提交于 2019-11-29 06:18:00

fprintf writes a string. fwrite writes bytes. So in your first case, you're writing the bytes that represent an integer to the file; if its value is "4", the four bytes will be in the non-printable ASCII range, so you won't see them in a text editor. But if you look at the size of the file, it will probably be 8, not 4 bytes.

Using printf() converts the integer into a series of characters, in this case "4". Using fwrite() causes the actual bytes comprising the integer value to be written, in this case, the 4 bytes for the characters 'w', 'o', 'r', and 'd'.

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