how to change data of struture object in file
-
Hello All , Sorry i am posting all code , i this code it is writting & reading stuct data ,BUt not changing that data i want change that data #include <iostream> #include <fstream> #include <cstring> using namespace std; struct MyRecord { char name[80]; double balance; unsigned long account_num; }; int main() { struct MyRecord acc; strcpy(acc.name, "R"); acc.balance = 1.3; acc.account_num = 34; ofstream outbal("balance", ios::out | ios::binary); if(!outbal) { cout << "Cannot open file.\n"; return 1; } outbal.write((char *) &acc, sizeof(struct MyRecord)); outbal.close(); ifstream inbal("balance", ios::in | ios::binary); if(!inbal) { cout << "Cannot open file.\n"; return 1; } inbal.read((char *) &acc, sizeof(struct MyRecord)); cout << acc.name << endl; cout << "Account # " << acc.account_num; cout.precision(2); cout.setf(ios::fixed); cout << endl << "Balance: $" << acc.balance; inbal.close(); return 0; }
-
Hello All , Sorry i am posting all code , i this code it is writting & reading stuct data ,BUt not changing that data i want change that data #include <iostream> #include <fstream> #include <cstring> using namespace std; struct MyRecord { char name[80]; double balance; unsigned long account_num; }; int main() { struct MyRecord acc; strcpy(acc.name, "R"); acc.balance = 1.3; acc.account_num = 34; ofstream outbal("balance", ios::out | ios::binary); if(!outbal) { cout << "Cannot open file.\n"; return 1; } outbal.write((char *) &acc, sizeof(struct MyRecord)); outbal.close(); ifstream inbal("balance", ios::in | ios::binary); if(!inbal) { cout << "Cannot open file.\n"; return 1; } inbal.read((char *) &acc, sizeof(struct MyRecord)); cout << acc.name << endl; cout << "Account # " << acc.account_num; cout.precision(2); cout.setf(ios::fixed); cout << endl << "Balance: $" << acc.balance; inbal.close(); return 0; }
ashish8patil wrote:
i this code it is writting & reading stuct data ,BUt not changing that data...
Of course it's not changing the data. You've shown no code to do that.
ashish8patil wrote:
...i want change that data
So change it and call the
write()
method again. What's the problem? :confused:"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hello All , Sorry i am posting all code , i this code it is writting & reading stuct data ,BUt not changing that data i want change that data #include <iostream> #include <fstream> #include <cstring> using namespace std; struct MyRecord { char name[80]; double balance; unsigned long account_num; }; int main() { struct MyRecord acc; strcpy(acc.name, "R"); acc.balance = 1.3; acc.account_num = 34; ofstream outbal("balance", ios::out | ios::binary); if(!outbal) { cout << "Cannot open file.\n"; return 1; } outbal.write((char *) &acc, sizeof(struct MyRecord)); outbal.close(); ifstream inbal("balance", ios::in | ios::binary); if(!inbal) { cout << "Cannot open file.\n"; return 1; } inbal.read((char *) &acc, sizeof(struct MyRecord)); cout << acc.name << endl; cout << "Account # " << acc.account_num; cout.precision(2); cout.setf(ios::fixed); cout << endl << "Balance: $" << acc.balance; inbal.close(); return 0; }
Plese use the code block button for posting code snippets.
ashish8patil wrote:
i this code it is writting & reading stuct data ,BUt not changing that data i want change that data
What do you mean with this? Your program sets the fields of the struct, then write it to disk, and finally read it again from disk. How these operations are supposed to change the content? What is your expected output? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]