从文本文件中读取的对象数组中最后丢失的数据
Last data missing in an object array read from text file
这是一个家庭作业项目,用户与数据库交互,数据库中有数据,如号码,街道名称等,这些数据驻留在文本文件中。
一些菜单选项要求程序读取或操作该数据,例如显示选项。我通过打开fstream并将数据放入数据类型为预先声明的结构体resident的临时数组来实现这一点。
//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();
My 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;
};
您正在读取nombreResid
项到内存中,但您的缓冲区resident tabRes1[nombreResid-1]
只有nombreResid-1
项的空间。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 读取文件的最后一行并输入到链接列表时出错
- 如何在Qt TableView中将列的宽度调整为数据并将最后一部分拉伸在一起
- 缓冲区用不需要的数据填充 char 数组中的最后一个空格
- 计算列表中与线性链接列表中最后一个节点具有相同数据的节点的数量
- 将数据附加到 csv 文件,在最后一个条目之前而不是最后一个条目之后.C++
- 为什么OpenDDS在删除DataWriter时重新发送相同的最后一个数据
- 在 Getline C++ 上复制最后的数据
- 正确的数据结构,用于在C++中存储最后 n 个元素
- 将数据从文件放入字符数组不会在最后数据停止
- 我似乎无法在qt创建器中打印几行数据。程序覆盖除最后一行之外的所有输出
- C++ OpenMP:将 for 循环拆分为静态偶数块,并在最后连接数据
- 从文本文件中读取的对象数组中最后丢失的数据
- 在整型数组的最后一个元素中存储数据的问题
- 有一个很长的缓冲区,但只使用最后1GB字节的数据
- 在列表中搜索数据的最后一个实例
- 使用Rcpp返回数据帧,其中仅包含例如患者的最后观察结果
- Gnuplot:跳过最后一行/不完整的数据文件