Click here to Skip to main content
15,500,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
struct st
int rollno;
char name[23];
float marks;
void main()
st  aa;
ifstream g;"fd2.dat",ios::binary);

this program is for reading data in binary format but my output is make no sense and display nonsense characters on the screen
Updated 19-Jun-13 7:00am
joshrduncan2012 19-Jun-13 11:51am    
So what is your question?
ZurdoDev 19-Jun-13 11:57am    
You need to be clear.
ALIWAZ 19-Jun-13 12:12pm     CRLF
i read data from binary format file but data is not displayd in text
Richard C Bishop 19-Jun-13 12:17pm    
You need to ask an actual question. All you have said is pretty much nothing helpful.
Cory Shirts 19-Jun-13 12:22pm    
His question is why is it displaying nonsense chars on the screen?

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.
Share this answer
nv3 19-Jun-13 17:49pm    
Exactly. It would help if OP displayed the contents of the binary file in hex representation. Then we could say something helpful about what went wrong. +5
CPallini 20-Jun-13 1:53am    
Thank you.
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.
Share this answer
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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900