Error in reading struct from binary file.

Below is the code for reading a struct that was stored in a binary file.
Problem is while reading from file I get the name without first character and age is always equal to zero which it should not be.
Please explain what's wrong with the code?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <conio.h>
#include <fstream>

struct abc
{
	char name[100];
	int age;
};


using namespace std;
int main()
{


	int op;
	cout<<"1-to enter data"<<endl;
	cout<<"2-to retrieve data "<<endl;
	


	cin>>op;

	switch(op)
	{
		case 1:
		{

			abc a;

			cout<<"enter name ";
			cin>>a.name;
			cout<<"Enter age ";
			cin>>a.age;

			ofstream outFile;
			outFile.open("info.dat",ios::binary|ios::app|ios::out);

			outFile.write((char*)&a,sizeof(struct abc));
			
			outFile.close();
		break;
		}

		case 2:
		{

			abc a;
			int age;
			cout<<"enter age ";
			cin>>age;

			ifstream inFile;
			inFile.open("info.dat",ios::binary|ios::app|ios::out);
			int counter=0,check=-1;
			while(!inFile.eof())
			{
				inFile.seekg(counter,ios::beg);
				inFile.read((char*)&a,sizeof(struct abc));

				if(age == a.age)
				{
					check = 1;					
				}
					counter++;

			}

			if(check == 1)
			{	
				cout<<"found at "<<counter<<endl;
				cout<<"name is "<<a.name<<endl;
				cout<<"age is "<<a.age;
			}


			inFile.close();
		break;
		}

	}
	_getch();
	return 0;
}
Last edited on
Try This



#include <iostream>
#include <conio.h>
#include <fstream>

struct abc
{
char name[100];
int age;
};


using namespace std;
int main()
{


int op;
cout<<"1-to enter data"<<endl;
cout<<"2-to retrieve data "<<endl;



cin>>op;

switch(op)
{
case 1:
{

abc a;

cout<<"enter name ";
cin>>a.name;
cout<<"Enter age ";
cin>>a.age;

ofstream outFile;
outFile.open("info.dat",ios::binary|ios::app|ios::out);

outFile.write((char*)&a,sizeof( abc));

outFile.close();
break;
}

case 2:
{

abc b;
int age;
cout<<"enter age ";
cin>>age;

ifstream inFile;
inFile.open("info.dat",ios::binary);
int counter=0,check=-1;
while(!inFile.eof())
{
inFile.read((char*)&b,sizeof( b));

if(age == b.age)
{
check = 1;
}
counter++;

}

if(check == 1)
{
cout<<"found at "<<counter<<endl;
cout<<"name is "<<b.name<<endl;
cout<<"age is "<<b.age;
}


inFile.close();
break;
}

}
_getch();
return 0;
}
I don't see why do you need to use seekg. You read data from file sequentially anyway. I'd recommend replacing lines 57-75 with
1
2
3
4
5
6
7
8
9
10
            while(inFile.read((char*)&a,sizeof(struct abc)))
            {
                if(age == a.age)
                {
                    cout<<"found at "<<counter<<endl;
                    cout<<"name is "<<a.name<<endl;
                    cout<<"age is "<<a.age;
                    break;
                }
            }


Or if you really want to use seek - then you seek at wrong location. You need to offset your read position by sizeof(abc), not by 1. Means that line 66 should look like counter+=sizeof(abc);. Plus in this case you still have flawed logic - if you found a record with the given age you only set the check = 1. On the next iteration of the while loop, the data in object b will be overwritten so your output will be wrong. To fix that you need to break the loop as soon as you've found a record with correct age - add a break; after line 64;
By the way, the version of the while loop I proposed can be easily changed for output of all records with the given age - just remove the break statement in line 8.
Topic archived. No new replies allowed.