English 中文(简体)
C++类对象向量到文件ack到程序
原标题:C++ vector of class objects to fileack to program

我期待着在每次重新启动程序时都能使用我的数据。我很好奇,存储到文件比读回程序的最佳方式是什么。我一直在网上读一些东西,最大的问题是XML还是二进制格式?我还在学习c++,我还没有掌握它。程序的对象类型是stringintint

还有一件事,有人知道这个到二进制到XML

很抱歉错过了代码部分,但我想知道一些比我更高级的程序员的意见。:P

最佳回答

除了Matthai的评论:

我认为最明显的格式是正确的,那就是纯文本。

只需以纯文本形式序列化数据(通常用空格分隔)。PT的优点是它是人类可读的、人类可修改的、易于使用流(>;>;标记化或增强标记器)处理的、灵活的,并且比XML轻得多。

例如,您可能想存储

struct {
    std::string name;
    int age;
    double height;
};

你只要写:

John 21 5.4
Bill 31 4.9

或者你有什么。这总是很方便,例如name可以包含两个单词,所以:

John Smith

标记器会在空格上拆分,并尝试将smith解析为int,但使用分隔符很容易解决这个问题。例如“”

问题回答

I do not agree. There are plenty options available. Two more i name you here: 1) You may look to a file format called json which has an own website (some of us don t do). It claims to be a lightweight data-interchange format. 2) There is a file-format called csv. The usage of which was already discussed on stackoverflow here

即使进程过早终止(例如,由于断电、硬件故障或代码本身存在严重错误),您是否也需要稳健的行为?

如果是这样,可以考虑使用“嵌入式”数据库,如SQLite或MS SQL Server Compact(等)。这些系统的事务性应该确保您不会最终得到损坏的数据,这会阻止您的程序正确启动。

此外,一些文件系统支持事务(例如Windows Vista或更高版本中的事务性NTFS)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?