Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to change data of struture object in file

how to change data of struture object in file

Scheduled Pinned Locked Moved C / C++ / MFC
iostutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashish8patil
    wrote on last edited by
    #1

    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; }

    D CPalliniC 2 Replies Last reply
    0
    • A ashish8patil

      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; }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A ashish8patil

        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; }

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups