issues with dat file

solved
Last edited on
My first suggestion would be to make your code readable by applying some consistent indentation.
https://en.wikipedia.org/wiki/Indentation_style

You attract more interest with honey rather than vinegar.

> system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
No, this is an awful approach to re-running the program.
Because you now have TWO copies of the program fighting over the same open files.
As one file and as a first refactor, consider:

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>

class Iesnc {
public:
	Iesnc(int = 0, const std::string & = "", const std::string & = "", double = 0.0);

	void setAccountNumber(int);
	int getAccountNumber() const;

	void setLastName(const std::string&);
	std::string getLastName() const;

	void setFirstName(const std::string&);
	std::string getFirstName() const;

	void setBalance(double);
	double getBalance() const;

private:
	int accountNumber {};
	char lastName[15] {};
	char firstName[10] {};
	double balance {};
};

Iesnc::Iesnc(int accountNumberValue, const std::string& lastNameValue, const std::string& firstNameValue, double balanceValue)
		: accountNumber(accountNumberValue), balance(balanceValue) {
	setLastName(lastNameValue);
	setFirstName(firstNameValue);
}

int Iesnc::getAccountNumber() const {
	return accountNumber;
}

void Iesnc::setAccountNumber(int accountNumberValue) {
	accountNumber = accountNumberValue;
}

std::string Iesnc::getLastName() const {
	return lastName;
}

void Iesnc::setLastName(const std::string& lastNameString) {
	auto length {lastNameString.size()};

	length = (length < 15 ? length : 14);
	lastNameString.copy(lastName, length);
	lastName[length] = '\0';
}

std::string Iesnc::getFirstName() const {
	return firstName;
}

void Iesnc::setFirstName(const std::string& firstNameString) {
	auto length {firstNameString.size()};

	length = (length < 10 ? length : 9);
	firstNameString.copy(firstName, length);
	firstName[length] = '\0';
}

double Iesnc::getBalance() const {
	return balance;
}

void Iesnc::setBalance(double balanceValue) {
	balance = balanceValue;
}

void outputLine(std::ostream&, const Iesnc&);
void writeToDat(std::fstream&);
void readFromDat(std::fstream&);
//void saveDatabase(std::fstream&);

int main() {
	std::fstream masterStream("master.dat", std::ios::in | std::ios::out | std::ios::binary);
	std::fstream saveLoadDatabase("databaseCopy.dat", std::ios::in | std::ios::out | std::ios::binary);

	if (!masterStream || !saveLoadDatabase)
		return (std::cout << "Cannot open files\n"), 1;

	for (int key {-1}; key != 0; ) {
		std::cout << "\n1 >> write\n";
		std::cout << "2 >> read\n";
		std::cout << "3 >> refresh\n";
		std::cout << "4 >> save database\n";
		std::cout << "0 >> exit\n";
		std::cout << "Enter option: ";
		std::cin >> key;

		switch (key) {
			case 1:
				writeToDat(masterStream);
				break;

			case 2:
				readFromDat(masterStream);
				break;

			case 3:
				std::cout << "Not yet implemented\n";
				//system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
				break;

			case 4:
				std::cout << "Not yet implemented\n";
				//cout << "save dataBase\n";
				//saveDatabase(saveLoadDatabase, client);
				break;

			case 0:
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	}
}

void outputLine(std::ostream& output, const Iesnc& record) {
	output << std::left << std::setw(10) << record.getAccountNumber()
		<< std::setw(16) << record.getLastName()
		<< std::setw(11) << record.getFirstName()
		<< std::setw(10) << std::setprecision(2) << std::right << std::fixed
		<< std::showpoint << record.getBalance() << '\n';
}

void writeToDat(std::fstream& masterStream) {
	if (!masterStream) {
		std::cerr << "File not opened.\n";
		return;
	}

	int accountNumber {};
	std::string lastName;
	std::string firstName;
	double balance {};

	do {
		do {
			std::cout << "Enter account number (1 to 100, 0 to end input): ";
			std::cin >> accountNumber;
		} while ((accountNumber < 0 || accountNumber > 100) && (std::cout << "Invalid account\n"));

		if (accountNumber != 0) {
			std::cout << "Enter last name, first name, balance: ";
			std::cin >> lastName;
			std::cin >> firstName;
			std::cin >> balance;

			const Iesnc client {accountNumber, lastName, firstName, balance};

			masterStream.seekp((accountNumber - 1) * sizeof(Iesnc));
			masterStream.write(reinterpret_cast<const char*>(&client), sizeof(Iesnc));
		}
	} while (accountNumber != 0);
}

void readFromDat(std::fstream& masterStream) {
	if (!masterStream) {
		std::cerr << "File not opened.\n";
		return;
	}

	std::cout << std::left << std::setw(10) << "\nAccount" << std::setw(16)
		<< "Last Name" << std::setw(11) << "First Name" << std::left
		<< std::setw(11) << std::right << "Balance\n";

	masterStream.seekg(0);

	for (Iesnc client; masterStream.read(reinterpret_cast<char*>(&client), sizeof(Iesnc)); )
		if (client.getAccountNumber() != 0)
			outputLine(std::cout, client);

	masterStream.clear();
}

/*
void saveDatabase(fstream& saveLoadDatabase, const Iesnc&) {
	cout << "saveDatabase" << endl;

	cout << left << setw(10) << "Account" << setw(16)
		<< "Last Name" << setw(11) << "First Name" << left
		<< setw(10) << right << "Balance" << endl;

	Iesnc client;
	saveLoadDatabase.read(reinterpret_cast<char*>(&client), sizeof(Iesnc));
	while (saveLoadDatabase && !saveLoadDatabase.eof()) {

		if (client.getAccountNumber() != 0)
			outputLine(cout, client);
		saveLoadDatabase.read(reinterpret_cast<char*>(&client),
			sizeof(Iesnc));
	}
}
*/


This can now be used as a starter to build upon.
salem c (3416)
My first suggestion would be to make your code readable by applying some consistent indentation.
https://en.wikipedia.org/wiki/Indentation_style

You attract more interest with honey rather than vinegar.

> system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
No, this is an awful approach to re-running the program.
Because you now have TWO copies of the program fighting over the same open files.



How can I re-open program in proper way?
I just need to dat file be refreshed before i do next operation
How can I re-open program in proper way?


You don't. If you need to re-execute part of main, then put that part in a loop.

I just need to dat file be refreshed before i do next operation


From what you seem to be doing with options 1) and 2) (see my revised code above), you don't need to refresh the master file as it is always up to date.

What's the purpose of databaseCopy file?


There is no need to reload anything.

If you use this
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
Like so
1
2
3
masterStream.seekp((accountNumber - 1) * sizeof(Iesnc));
masterStream.write(reinterpret_cast<const char*>(&client), sizeof(Iesnc));
masterStream.flush();

you should be able to toggle between write and read as much as you want.

Even if you did want to go round again, you would use a while loop to do it.
solved
Last edited on
@OP As you don't seem to have read/listened what has been said in previous posts by myself and others, and persist in doing things that we have said don't - eg L46,52,57, then I see no point in continuing to provide advice/guidance. I'm out.
seeplus, i'm still using header and two cpps, but i optimized a lot as per your advise!
Stream tools researched and implemented. Left one execution to refresh file manually :)


Thank you bro!
I finished everything here, save load dat included :)
Many thanks for your advice!
Now i'm going for GUI with QT :D
BRB once I get back to backend part.
Thank you again, you were very helpful for a lot of cases.
I see impetus is another one of these delete and scarper types.

Deleting posts makes the thread less useful for anyone finding the thread later.
There's little point in us responding if the result is going to look like disjointed babble.
I flushed my tag in web, can you please delete the code?



switch (key) {
case 1:
writeToDat(masterStream);
break;

case 2:
readFromDat(masterStream);
break;

case 3:
std::cout << "Not yet implemented\n";
//system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
break;

case 4:
std::cout << "Not yet implemented\n";
//cout << "save dataBase\n";
//saveDatabase(saveLoadDatabase, client);
break;

case 0:
break;

default:
std::cout << "Invalid option\n";
break;
}
}
}



Last edited on
I see we have another "I don't want to let others see what I did so I edit my posts," troll.
I flushed my tag in web, can you please delete the code?


It's not etiquette - or polite - to remove/amend posts once an answer has been provided. Posts are part of the thread of a topic. So no, the code (my code) outside of your posts won't be deleted.

And I doubt that in future you'll receive much in the way of help with further questions.
Last edited on
Ok, ok :)
it was actually my code, so lets just divide this into two parts :).

Just leave the part where I actually asked question:


switch (key) {
case 1:
writeToDat(masterStream);
break;

case 2:
readFromDat(masterStream);
break;

case 3:
std::cout << "Not yet implemented\n";
//system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
break;

case 4:
std::cout << "Not yet implemented\n";
//cout << "save dataBase\n";
//saveDatabase(saveLoadDatabase, client);
break;

case 0:
break;

default:
std::cout << "Invalid option\n";
break;
}
}

That gotta be fair. Because the rest of code doesn't make a sense for random reader.
Topic name is also implies there were issue with DAT file, which is never connected to 90% of code.
Still under your consideration, but I'm asking you as a friend and frequent forum user, lets divide the case into two pieces


P.S Next time I will never edit like this and keep politeness level as per your kind advise

Namaste
Someone burned a bridge too far for us to reply.
Topic archived. No new replies allowed.