关于处理读取功能的问题 [英] question about handling read function

查看:44
本文介绍了关于处理读取功能的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
struct st
{
int rollno;
char name[23];
float marks;
};
void main()
{
clrscr();
st  aa;
ifstream g;
g.open("fd2.dat",ios::binary);
while(!g.eof())
{
g.read((char*)&aa,sizeof(aa));
cout<<aa.rollno<<endl;
cout<<aa.name<<endl;
cout<<aa.marks;
}
g.close();
}







这个程序用于读取二进制格式的数据,但我的输出是make没有任何意义,并在屏幕上显示无意义的字符




this program is for reading data in binary format but my output is make no sense and display nonsense characters on the screen

推荐答案

可能你的二进制文件包含'没有意义'。首先要确保文件内容是正确的(例如,您可以使用十六进制编辑器打开它),然后 debug 您的程序以查找 aa 变量填充了正确的数据。
Probably your binary file contains 'no sense'. You have first to be sure the file content is correct (you might, for instance, open it with an hex editor) and then debug your program to find out if the aa variable is filled with correct data.


您正在读取的数据大小可能不对齐,因此内存条表示结构正在填充以保留它。这可能是数据出现乱码的原因。


结构大小为31个字节。您可以尝试将char数组的大小增加一个以对齐它。
The size of the data you're reading is probably out of alignment, so the piece of memory representing the struct is being padded to preserve it. This may be the reason data is being garbled.

The struct is 31 bytes in size. You could try increasing the size of the char array by one to align it.


1。可能(尽管不太可能)原因:无法正确恢复上下文数据



尝试将原始数据复制到C ++中的结构或类的地址或从中复制原始数据是危险且容易发生的意外问题:要么可能包含定义中定义的字段之外的其他上下文数据,并且无法保证以二进制形式保存和恢复其他信息将起作用。最值得注意的是,如果涉及任何指针,这些肯定会恢复到合理的价值。



唯一安全的保存方式并且用C ++恢复数据结构是为了单独处理每个数据字段。



在你的情况下,它可能有用,因为struct不包含任何方法,也没有派生类从那个结构。但是,除非另有说明,否则编译器通常会为没有明确声明的类和结构添加构造函数和析构函数。如果自动创建的析构函数被声明为虚拟(它应该),那么该类的每个实例也必须包含一个虚拟函数表,实际上不能保存和恢复!



所有这些都做了很多假设,但它可能会导致你描述的问题。也就是说,据我所知,所有编译器都豁免这种简单的情况,如果实际上没有涉及虚函数或派生类,则不会创建VFT。



2可能的原因:不适当的文件格式



如果数据文件的创建方式与您读取的方式不完全相同,则恢复的结构可能包含垃圾。您可以将用于保存数据的代码发布到文件中,或者更好地查看文件中包含的数据,这样您就可以验证数据是否以您期望的方式存储。



3.可能的原因:数据损坏



您是否确认数据文件正常并且没有以某种方式损坏?是否有合适的尺寸?



4.可能的原因:不同的二进制格式



如果你使用的话程序存储数据的不同编译器,或者数据存储在具有不同操作系统的机器上,二进制格式可能与机器上的格式不同。例如,一些OS和/或编译器首先存储整数值的高字节,其他OS最后存储它们。有些使用4个字节,有些是8.如果这可能是问题,请尝试以文本模式保存(和恢复)数据。
1. possible (albeit unlikely) cause: failed to properly restore contextual data

Trying to copy raw data to or from the address of a struct or class in C++ is dangerous and prone to unexpected problems: either may contain additional contextual data beyond the fields defined in your definition, and there is no guarantee that saving and restoring that additional info in binary form will work. Most notably, if any pointers are involved, these will most certainly not be restored to a sensical value.

The only safe way to save and restore data structures in C++ is to treat each data field individually.

In your case, it might work, because the struct contains no methods and there is no class derived from that struct. However, unless specified otherwise, the compiler will usually add constructors and destructors for classes and structs that don't have an explicitely declared one. And if the automatically created destructor is declared virtual (which it should), then every instance of that class must also contain a virtual function table, which can indeed not be saved and restored!

All this makes a lot of assumptions, but it could cause the problem you describe. That said, to my knowledge all compilers take exemption to such simple cases and won't create VFTs if there are in fact no virtual functions or derived classes involved.

2. possible cause: inappropriate file format

If the data file was not created in a way that exactly mirrors the way you read it, your restored structure will likely contain garbage. You can post the code used to save the data to file, or, better yet, look at the data contained in your file, so you can verify that the data is stored in exactly the way you expect it.

3. possible cause: data corrupted

Have you verified that the data file is ok and not in some way damaged? Does it have the right size?

4. possible cause: different binary formats

If you used a different compiler for the program to store the data, or the data was stored on a machine with a different OS, chances are that the binary format differs from that on your machine. For example some OSs and/or compilers store the high bytes of an integer value first, others store them last. Some use 4 bytes, some 8. If this could be the problem, try saving (and restoring) the data in text mode instead.


这篇关于关于处理读取功能的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆