C++使用运算符>>序列化文件
我正在使用友元关键字和运算符重载(>> <<)使用 C++ 编写第一个序列化程序。
我存储序列化对象的文件称为 file.CARS,当然用于存储汽车对象。
我是这样创建的:
std::ofstream output(file.CARS, std::ios::binary);
文件已创建,我可以毫无问题地存储我的对象。无论我发现了什么:
该文件名为 file.CARS,我认为 ios::binary 和“未知”扩展名 (*.CARS) 将帮助我防止使用简单的文本编辑器打开它。
我错了......内容可以通过简单的文本编辑器查看和修改......
有没有办法可以“保护”这个文件?
非常感谢您的帮助。
I'm doing my first serialization programs with C++ using the friend keyword and operator overloading (>> <<).
The file in which I store the serialized objects is something called file.CARS, for storing car objects of course.
I created it this way :
std::ofstream output(file.CARS, std::ios::binary);
The file is created and I can store there my objects with no problem. Whoever I discovered something:
The file is called file.CARS and I thought that the ios::binary and "unknown" extension (*.CARS) will help me prevent it from being opened with a simple text editor.
I was wrong... and the content can be seen and modified by a simple text editor...
Is there a way I can "protect" this file?
Thanks a lot for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看问题的答案 如何在 C++ 中序列化对象? 如果您只是想混淆数据,
boost::archive::binary_oarchive
可能会成功。Check out the answer to the question How do you serialize an object in C++? If you're just trying to obfuscate your data,
boost::archive::binary_oarchive
will probably do the trick.不使用标准 C++。
binary
标志只是说明如何处理行结尾。 所有文件都是二进制格式 - 这就是计算机的全部意义!您可能想阅读加密。Not using standard C++. The
binary
flag simply says how line-endings are handled. And all files are in binary format - that's what computers are all about! You probably want to read up on encryption.如果您存储 ASCII 字符串,那么转换为二进制实际上不会执行任何操作...ASCII 字符串已经存储为二进制数据,即“hello world”,在实际内存中存储为(十六进制)
:将这些内存字节写入磁盘,无论是 ASCII 模式还是二进制模式,最终都会得到存储在磁盘内存中的相同字符串。因此,任何要从磁盘读取内存并重新解释字节的简单文本编辑器都只会打印出您保存的 ASCII 字符串。
如果您不想让别人打开您的文件并读取您的 ASCII 字符串,那么您将不得不以某种方式对它们进行打乱。如果加密太严厉,您可以做一些简单的事情,例如交换符号位(对于带符号的 ASCII
char
),将值偏移一定量(注意换行)周围),进行一些轻量级游程编码压缩等。只要确保当您读回值时,您正确地反转了最初应用于数据的任何转换。If you're storing ASCII strings, then converting to binary doesn't really do anything ... the ASCII strings are already stored as binary data, i.e., "hello world", in actual memory is stored as (in hex):
When you write those memory bytes to disk, whether it's an ASCII mode, or binary mode, you're going to end up with the same string stored in the memory of the disk. So any simple text editor that is going to read that memory off the disk and re-interpret the bytes, will just print-out the ASCII string that you saved.
If you don't want to have someone open your file and read your ASCII strings, then you're going to have to scramble them somehow. If encryption is too heavy-handed, you can do something simple like swapping the sign-bit (for an ASCII
char
, which is signed), offsetting the values by a certain amount (watch out for wrap-around), do some light-weight run-length-encoding compression, etc. Just make sure when you read the values back, you properly reverse whatever transform you originally applied to the data.