Trouble Removing and Renaming Binary Files

I tried to write a Program to Insert Information of an Item to an existing Binary File. But something is fishy. The Compiler shows NO ERROR.

The New Records (with the inserted record) is found under "temp.dat". But it isn't found in "stores.dat".

As I could SEE my old (before inserting) content in my "stores.dat", it means "stores.dat" isn't deleted either!

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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: "; cin>>ws; std::cin.getline(iname,100,'\n');
		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 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));
  }
  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;
  }
};

void OpenFile()
{
   system("CLS");
char str[100];
   store s;
cout<<"\n\nEnter File Name to display: "; cin>>ws; std::cin.getline(str,100,'\n');
   ifstream f1;
   f1.open(str, ios::binary);
   cout<<"\n\nShowing Contents of the File";

	while(s.LoadFromStream(f1))
	{

	   s.Details();
	}
	f1.close();
}

void InsertEntry()
{
	system("CLS");
	store snew,sold;
	ifstream f1;
	f1.open("stores.dat", ios::binary);
	ofstream f2;
	f2.open("temp.dat", ios::binary);
	cout<<"\nYou're about to Insert a New Item in the existing File";
	snew.NewItem();
	char last='y';
	while(sold.LoadFromStream(f1))
	{

		if(snew.returnIno()<=sold.returnIno())
		{
			snew.SaveToStream(f2);
			last='n';
			break;
		}
		else
		{
			sold.SaveToStream(f2);
		}
	}

	if(last=='y')
    {
        snew.SaveToStream(f2);
    }
    else
    {
        while(sold.LoadFromStream(f1))
        {

            sold.SaveToStream(f2);
        }
    }
    f1.close();
	f2.close();
	remove("stores.dat");
	rename("temp.dat","stores.dat");
	OpenFile();
}

Coded in Code Blocks, minGW compiler.
Last edited on
"something is fishy" is pretty unspecific. Can you tell us what is happening vs. what you expect to happen?
Whenever you open a file for output, you truncate it, which, if the file is supposed to already exist, does seem a little fishy.
doug4, The Compiler shows NO ERROR. But the Record details (that the user inputs) doesn't seem to be stored in the file, as it doesn't show up on reading the file later.
cire, I open my old file in"ifstream" that doesn't truncate the file. My new file is "new", so there is no problem truncating it!
Sorry, After a self-compilation, I found that the trouble isn't in Inserting but in Removing and Renaming. Please see the updated info above.

Topic archived. No new replies allowed.