file operation in binary vs text mode — performance concern

前端 未结 7 663
执笔经年
执笔经年 2020-12-31 10:04

In many projects, I saw that data object/structure are written into file in binary mode, and then retrieve them back from the file in binary mode again.

I wonder why

7条回答
  •  梦谈多话
    2020-12-31 10:36

    If your program is the only program that is going to use the file, you can save internal structures "as is" using binary files.

    However, if you want to exchange the files with other programs, or over the Internet, then binary formats are not that good. Think for example about the problem with big-endian vs. little-endian machines. Also, the receiver of the files or data will most likely not have access to your code and your structures, so a text-based format might be easier to parse and implement into own structures.

    About performance, it's true that reading and writing your internal structures directly will be quicker, because you don't have to translate them (also known as marshaling) into another format.

提交回复
热议问题