【发布时间】:2014-08-10 05:09:19
【问题描述】:
这是一个家庭作业项目,用户在其中与具有数字、街道名称等数据的人员数据库进行交互,这些数据位于文本文件中。
某些菜单选项需要程序读取或操作该数据,例如显示选项。为此,我打开了一个 fstream 并将数据放入一个临时数组,该数组的数据类型为预先声明的 struct 常驻。
//reopen file
fstream listeResidents1;
listeResidents1.open("listeResidents.dat",ios::out|ios::in|ios::binary);
//Create a temporary array of objects resident for manipulation
resident tabRes1[nombreResid-1];
//Insertion des objets resident du registre dans le tableau temporaire
for (int h = 0;h < nombreResid;h++)
{
listeResidents1.seekg((h)*sizeof(resident));
listeResidents1.read(reinterpret_cast<char*>(&tabRes1[h]),sizeof(resident));
}
listeResidents1.close();
我的 Display() 函数执行此操作^^^,然后使用 for 循环遍历数组中的每个对象并显示它们的所有属性。然而,最后一个条目的倒数第二个字符串缺少一些字符,最后一个变量,一个浮点数,设置为 0(这意味着它也被截断),我不知道为什么。有谁知道为什么会这样?
对于一个视觉示例,最后一个条目已成功写入具有这些属性的文件:
int numero: 4
[the other,correctly displayed attributes here]
char rue[30]: queen
float superf: 80
显示最后一个条目时会显示
numero: 4
[the other,correctly displayed attributes here]
rue: que
superf: 0
如果我添加另一个条目,它会被截断,而现在 4 正确显示...
编辑:这是结构。
struct resident
{
int numero;
char nom[30];
char prenom[30];
int numciv;
char rue[30];
float superf;
};
【问题讨论】:
-
您需要向我们展示类/结构常驻者的定义。
-
也显示写的代码。
标签: c++ arrays struct text-files truncated