Writing into a Binary File

closed account (9GEqko23)
Hello.
I seem to encounter a problem where I can't write the struct into my binary file for my program. It writes into the struct fine but when I close the program and open the binary file and output the data, it outputs nothing.

main (Basically just the menu)
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
int main()
{
	Hardware records[100];	

	int count = 0;
	int input;
	bool truth = false;
	fstream file;

	int fileInput = fileOpen(file, records, count);

	if(fileInput == 1) //If a new file was made
	{
		newInput(file, records, count);
	}

	else //If there is an existing file
		cout << "Existing File" << endl;
	do {
		cout << "1. List all tools." << endl;
		cout << "2. Update a record." << endl;
		cout << "3. Insert a record." << endl;
		cout << "4. Delete a record." << endl;
		cout << "5. End Program." << endl;
		cout << "? ";
		cin >> input;

		if(input == 1)
		{
			cout << input << endl;
			output(file, records, count);
			truth = false;
		}

		else if(input == 2)
		{
			cout << input << endl;
			truth = false;
		}

		else if(input == 3)
		{
			cout << input << endl;
			truth = false;
		}

		else if(input == 4)
		{
			cout << input << endl;
			truth = false;
		}

		else if(input == 5)
		{
			cout << input << endl;
			truth = true;
		}

		else
			truth = true;
	} while(truth == false);

	file.close();
	
	return 0;
}


Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

struct Hardware
{
	int num;
	char name[30];
	int quantity;
	double price;
};

int fileOpen(fstream& file, Hardware records[], int& count);

void newInput(fstream& file, Hardware records[], int& count);

void output(fstream& file, Hardware records[], int count);


My file with all of the functions, I think the problem lies within here:
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "hardware.h"

using namespace std;

int fileOpen(fstream& file, Hardware records[], int& count)
{
	file.open("hardware.dat");
	char input;
	bool truth = false;

	if(file.fail())
	{
		cout << "File cannot be opened. Create a new empty file" << endl;
		file.open("hardware.dat", ios::out);
		file.open("hardware.dat", ios::in | ios::out | ios::binary);

		file.clear();
		file.seekg(0, file.beg);

		for (int i = 0; i < 100; i++) //Writes into the binary file, making it empty
			file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));

		return 1; //If a new file is made
	}

	else if(file.good())
	{
		do {	cout << "Should the file be initialised (Y or N): ";
			cin >> input;
			if (input == 'Y' || input == 'y') //Clears Binary File
			{
				file.open("hardware.dat", ios::out);
				file.open("hardware.dat", ios::in | ios::out | ios::binary);

				file.clear();
				file.seekg(0, file.beg);

				for (int i = 0; i < 100; i++)
			file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
				cout << "Yes" << endl;
				truth = true;
			}

			else if (input == 'N' || input == 'n') //Reads from existing binary file
			{
				for (int i = 0; i < 100; i++)
				{
				file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
					if (records[i].num > 0)
						count++;
				}
				cout << "No" << endl;

				truth = true;
			}

			else
			{
				cout << "Invalid Input" << endl;
				truth = false;
			}
		} while(truth == false);
	return 2; //If there is an existing file
	}
}

void newInput(fstream& file, Hardware records[], int& count)
{
	//At the beginning of every function, read from the binary file
	for (int i = 0; i < 100; i++)
	{
	file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
	}

	int numLoop = 0;	

	for (int i = 0; i < 100; i++)
	{
	bool truth = false;

	cout << "Enter the part number (0-99, -1 to end the input): ";
	do {
		cin >> numLoop;
	
		if(numLoop > 99)
		{
			cout << "Invalid Input. Try Again" << endl;
			truth = false;
		}

		else
		{
			truth = true;
		}
	} while(truth == false);

	if (numLoop < 0)
		break;

	else
		records[i].num = numLoop;

	cin.ignore();

	cout << "Enter the tool name: ";
	cin.get(records[i].name, 30, '\n');

	cin.ignore();

	cout << "Enter the quantity: ";
	cin >> records[i].quantity;
	
	cin.ignore();

	cout << "Enter the price: ";
	cin >> records[i].price;

	count++;
	}

	for (int i = 0; i < count; i++)
		file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
}

void output(fstream& file, Hardware records[], int count)
{
	for (int i = 0; i < count; i++)
	file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
	file.clear();
	file.seekg(0, file.beg);
	int location = file.tellg();
	cout << "Current Location: " << location << endl; 
	for (int i = 0; i < count; i++)
	{
	cout << count << '\t' << records[i].num << '\t' << records[i].name << '\t';
	cout << records[i].quantity << '\t' << records[i].price << endl;
	}
}
Last edited on
closed account (9GEqko23)
Can anyone respond ASAP? Please, I need to get this done.
but when I close the program and open the binary file and output the data, it outputs nothing.

How are you opening the file to output the data?

Also you seem to be trying to open the same file with the same stream instance multiple times in your fileOpen() function. IMO that function is trying to do way too much. You should probably break that function up into several smaller functions. Seeing how you're actually declaring and initializing the variables used as parameters.

The following doesn't really make much sense:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	if(file.fail())
	{
		cout << "File cannot be opened. Create a new empty file" << endl;
		file.open("hardware.dat", ios::out);   // LINE 14
		file.open("hardware.dat", ios::in | ios::out | ios::binary);  // LINE 15

		file.clear();
		file.seekg(0, file.beg);

		for (int i = 0; i < 100; i++) //Writes into the binary file, making it empty
			file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));

		return 1; //If a new file is made
	}


First the file should be already open (see line 7). Second if you want to erase the file contents it would be easier to open the file in a mode that erases the file contents automatically. Also if the open failed from line 7 it will still fail on line 14 and line 15 could be trying to open an already open file, if line 14 did actually succeed.

Topic archived. No new replies allowed.