Read Binary File Records Show Only Junk Values

Jul 11, 2016 at 5:54pm
I tried to write a Program to store Info of Items in a Binary File. But except the 1st record, all other Records read thro' ShowFile() show only junk value. I have been trying to find the problem, but my code seems to be error-free to me. So, fellow Programmers help your newbie, find his Mistakes! (Note: I used Turbo C++ for compiling). Here is 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
#include <fstream.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

class store
{
	int ino, stock;
	char iname[100];
	float price;
	char chline;

	public:
	void NewItem()
	{
		cout<<"\n\nNew Item Registration:\n\nItem No.: "; cin>>ino;
		cout<<"\nItem Name: "; gets(iname);
		cout<<"\nPrice: "; cin>>price;
		cout<<"\nQuantity: "; cin>>stock;
		cout<<"\nRegistered!";
	}

	int returnIno()
	{
		return ino;
	}

	void Details()
	{
		cout<<"\n\nItem No.: "<<ino<<"\nItem Name: "<<iname<<"\nPrice: "<<price<<"\nStock: "<<stock;
	}
};


void NewFile()
{
    store s;
    ofstream f1;
    f1.open("stores.dat",ios::binary);
    char ch;
	cout<<"\n\nFresh file \"stores.dat\" created! Any previous content of any file of the SAME NAME is deleted!";
    do
    {
		s.NewItem();
		f1.write((char*)&s, sizeof(s));
		f1<<endl;
		cout<<"Do you want to Add more Items > (y/n):\t";
		cin>>ch;
    }while (ch=='y');
    cout<<"All Entries have been Stored in the File successfully!";
	f1.close();
}

void ShowFile()
{
	clrscr();
	store s;
	ifstream f1;
	f1.open("stores.dat", ios::binary);
	cout<<"\n\nShowing Contents of the File";
	f1.seekg(0);
	while(f1.read((char*)&s,sizeof(s)))
	{
			s.Details();
	}
	f1.close();
}

int main()
{
    clrscr();
	int choice;
	cin>>choice;
	if(choice==1) NewFile();
	else if(choice==2) ShowFile();
	else exit(0);
	getch();
}
Last edited on Jul 11, 2016 at 5:55pm
Jul 11, 2016 at 6:37pm
Line 46
 
        f1<<endl;
looks like a problem.
I suggest that line is deleted, it isn't matched by any corresponding file input in the ShowFile() function, which would explain why things get out of sync after the first record.
Last edited on Jul 11, 2016 at 6:38pm
Jul 11, 2016 at 8:22pm
I would read and write it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void SaveToStream (ofstream& os)
  {
    os.write ((const char *)&ino, sizeof (ino));
    os.write ((const char *)&stock, sizeof (stock));
    os.write ((const char *)iname, sizeof (iname));
    os.write ((const char *)&chline, sizeof (chline));
    os.write ((const char *)&price, sizeof (price));
  }
  void LoadFromStream (ifstream& is)
  {
    is.read ((char *)&ino, sizeof (ino));
    is.read ((char *)&stock, sizeof (stock));
    is.read ((char *)iname, sizeof (iname));
    is.read ((char *)&chline, sizeof (chline));
    is.read ((char *)&price, sizeof (price));
  }


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
New Item Registration:

Item No.: 1

Item Name: Hammer

Price: 10

Quantity: 10

Registered!

Item No.: 1
Item Name: Hammer
Price: 10
Stock: 10

Showing Contents of the File

Item No.: 1
Item Name: Hammer
Price: 10
Stock: 10
Jul 16, 2016 at 9:10pm
Thomas1965, Where did you insert those two functions of yours in the Code?
Last edited on Jul 16, 2016 at 9:11pm
Jul 17, 2016 at 8:51am
Actually I created a seperate test project for it. In your program I would add them to the class store and call them in NewFile and ShowFile.
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
void ShowFile()
{
   clrscr();
   store s;
   ifstream f1;
   f1.open("stores.dat", ios::binary);
   cout<<"\n\nShowing Contents of the File";

	while(f1)
	{
           s.LoadFromStream(f1);
	   s.Details();
	}
	f1.close(); // not needed
}

void NewFile()
{
    store s;
    ofstream f1;
    f1.open("stores.dat",ios::binary);
    char ch;
	cout<<"\n\nFresh file \"stores.dat\" created! Any previous content of any file of the SAME NAME is deleted!";
    do
    {
		s.NewItem();
		s.SaveToStream(f1);
		cout<<"Do you want to Add more Items > (y/n):\t";
		cin>>ch;
    }while (ch=='y');
    cout<<"All Entries have been Stored in the File successfully!";
	f1.close(); // not needed
}
Jul 17, 2016 at 7:19pm
Oh Yeah! Thanks Thomas1965. The code is finally working!

But Only one Problem.
Using while(f1) reads the last file two times. And using while(s.LoadFromStream(f1)) also seems to be an error!
So, Is there another way to counter the problem!
Jul 17, 2016 at 8:32pm
And using while(s.LoadFromStream(f1)) also seems to be an error!

Your idea was right - just modify LoadFromStream, then it should work.

1
2
3
4
5
6
7
8
9
10
ifstream& LoadFromStream (ifstream& is)
  {
    is.read ((char *)&ino, sizeof (ino));
    is.read ((char *)&stock, sizeof (stock));
    is.read ((char *)iname, sizeof (iname));
    is.read ((char *)&chline, sizeof (chline));
    is.read ((char *)&price, sizeof (price));

    return is;
  }


BTW. Your input need some fixing as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void NewItem ()
  {
    cout << "\n\nNew Item Registration:\n\nItem No.: "; 
    cin >> ino;
    cin.ignore (255, '\n');
    cout << "\nItem Name: "; 
    gets (iname);
    cout << "\nPrice: "; 
    cin >> price;
    cin.ignore (255, '\n');
    cout << "\nQuantity: "; 
    cin >> stock;
    cin.ignore (255, '\n');
    cout << "\nRegistered!";
  }
Jul 18, 2016 at 1:36pm
Thank you, Thomas1965. The code is now Perfect!

For the NewItem(), I had problem only with reading the string. And cin>>ws; helped it out.

So, is it okay or do you want me to fix my Code as you've posted. Also, a small explanation for such a fix, would help me increase my skills too!

And anyway, THANK YOU so much for such great support!
Last edited on Jul 18, 2016 at 1:37pm
Jul 18, 2016 at 3:56pm
If it works then there is nothing to fix.
A common problem with cin is that '\n' remains in the input buffer and the next input is skipped.
Normally you use cin.ignore (255, '\n'); to remove the '\n' from the input buffer.
Jul 18, 2016 at 6:48pm
Oh! Never knew that! Thanks for sharing a piece of knowledge... :)
Once again, THANK YOU!!!!!!
Jul 18, 2016 at 7:23pm
As a continuation, I tried to Insert records to my file. IDK why, the code doesn't seem to be working :(

I posted it as a seperate Query. Here is the link:
http://www.cplusplus.com/forum/general/194469/#msg935513
Last edited on Jul 18, 2016 at 7:23pm
Topic archived. No new replies allowed.