5 Replies - 310 Views - Last Post: 20 April 2013 - 06:17 AM Rate Topic: ***-- 2 Votes

#1 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 02:57 AM

Hi everyone. I got a problem during binary file handling. So I need to read information from a file as a binary format and keep the information in an array of struct. And then I need to display the information. When I read and display, it shows all the garbage values. Not even a single person information is displayed correctly. I think this is the problem during the reading from a file. So what is the problem of the code?

Here the code of mine

#include <iostream>
#include <fstream>

using namespace std;

struct details {
	char name[100];
	long int id;
	long int nic;
	char marital[50];
	int children;
	int gross;
	float pension;
	
	};

int main()
{
	details x[100];
	
	ifstream ins("a",ios::in|ios::binary);
	if(ins.good())
	{
		for(int i=0; i<100; i++)
		{
			ins.read((char*)&x[i], sizeof(details) );
		}
		
		
		for(int i=0; i<100; i++)
		{
			cout<<x[i].name<<endl;
			cout<<x[i].id<<endl;
			cout<<x[i].nic<<endl;
			cout<<x[i].marital<<endl;
			cout<<x[i].children<<endl;
			cout<<x[i].gross<<endl;
			cout<<x[i].pension<<endl;
		}
			
			
		
		ins.close();
	}
	return 0;
}



Here is the sample text file information

John 283563 896325 single 0 4800 4
Nikita 256896 454555 married 2 5200 3

Is This A Good Question/Topic? 0
  • +

Replies To: Problem while reading an information from a file as a binary format.

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 04:48 AM

Quote

I got a problem during binary file handling.

Quote

Here is the sample text file information

So is it a text file or a binary file?

Are you sure all the read operations are successful? Add some more ins.good() checks to your loop.
Was This Post Helpful? 0
  • +
  • -

#3 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Re: Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 05:51 AM

View PostHezekiah, on 20 April 2013 - 04:48 AM, said:

Quote

I got a problem during binary file handling.

Quote

Here is the sample text file information

So is it a text file or a binary file?

Are you sure all the read operations are successful? Add some more ins.good() checks to your loop.


yeah it won't work. cout gives all the garbage value
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,290
  • Joined: 25-December 09

Re: Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 05:51 AM

Quote

I think this is the problem during the reading from a file.

How did you write this information to your file? Did you view your file with a hex editor/viewer to insure the file was properly written?

What happens if there is less that 100 items in your file?

It would be better to use the return value of your read operation to control a loop when reading your file. For example:
	if(ins.good())
	{
      int j = 0;
      while(j < ARRAY_SIZE && ins.read((char*)&x[j], sizeof(details)))
         ++j;



The above code will read at most ARRAY_SIZE entries and will also stop processing when the end of file, or some other read error occurs.

Your code, with the above modifications seems to work properly.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Re: Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 06:10 AM

where do I have to keep this code. Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,290
  • Joined: 25-December 09

Re: Problem while reading an information from a file as a binary format.

Posted 20 April 2013 - 06:17 AM

Quote

where do I have to keep this code. Thanks for your help


What? I don't really understand your question. But you replace your "read" loop with the read loop above.

But you also need to show how you wrote this information to your file in the first place. If it wasn't written properly then you won't be able to read it back.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1