问题描述
我需要将一个类写入一个二进制文件,然后我需要读回它.
I need to write a class to a binary file, and then I need to read it back.
我有 Triangle
和 BinaryFile
类,以及其他一些类.我不确定我是写错了还是读错了.读取时出现错误.调试后,我认为它为我的私有变量获取了不适当的数据.如果有人能给我一些有关如何使其正常工作的建议,我将非常高兴.
I have Triangle
and BinaryFile
classes, and some other classes. I am not sure if I am writing incorrectly or reading incorrectly. An error occurs when reading. After debugging, I think that it gets inappropriate data for my private variables. I will be very glad if someone can give me some advice on how to make it work properly.
我不确定是否应该粘贴整个代码,所以我会给你一小段代码.以防万一,这里是我的源代码的下载链接:
I wasn't sure if I should paste the whole code or not, so I will give you a short snippet of code. Just in case, here is a download link for my source code:
https://my.pcloud.com/publink/show?code=XZJ7CYZbsLWLglqsV5jp83csijcEUTFqqpM3k/a>
https://my.pcloud.com/publink/show?code=XZJ7CYZbsLWLglqV5p83csijcEUTFqqpM3k
我是编程新手,我的英语不太好,所以我为我的错误提前道歉.
I am a newbie in programming and I don't speak English very well, so I apologize in advance for my mistakes.
推荐答案
答案取决于您这样做是为了了解文件的工作原理,还是保存到文件只是偶然的,您并不关心它是如何工作的.
The answer depends on whether you are just doing this to learn how files work or whether saving to the file is just incidental and you don't care how it works.
如果您只想保存和恢复内容,而不关心它是如何工作的,那么请使用第三方库.有很多很多.
If you just want to get the stuff to save and restore and you don't care how it works then use a third party library. There are many many of them.
如果你想学习如何读写文件,那么你需要自己编写读写函数.我制作了一个示例程序来解释它是如何工作的:
If you want to learn how to read and write things to files then you will need to make your own read and write functions. I have made a sample program that will explain how it works:
这篇关于c ++从/到二进制文件读/写类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!