Thread: help me to write in a file using a function

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Question help me to write in a file using a function

    hi
    i have created a class named BANK having three variables .i have created a object B . now using a function i want to store the corresponding values in a file.also using another function i want to read all the variables from the file.and want to print them in the console .for eg.

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>

    fstream fs;

    class bank
    {
    private :
    int no;
    char name[15];
    int bal;
    public :
    void getdata();//to read the variables
    void wwrite();//to write in a file
    void putdata();//to print in a console
    void rread();//to read from a file
    };
    void bank :: getdata()
    {
    cout << "\nenter the no name and balance\n";
    cin >> no >> name >> bal;
    }
    void bank :: putdata()
    {
    cout << "\nno\t: "<<no << "\nname\t: "<<name << "\n bal\t: "<< bal;
    }

    //to write inside a file

    void bank:: wwrite()
    {
    fs.open("c:\\files\\e8.txt",ios::app);
    fs.write((char *)&this,sizeof(this));
    fs.close();
    }

    void bank::rread()
    {

    fs.open("c:\\files\\bank.txt",ios::in);
    while (!fs.eof())
    {
    fs.read((char *)br,sizeof(br));
    cout <<"\n" << br.no << "\t" << br.name;
    }
    fs.close();
    }

    void bank :: putdata()
    {
    cout << "\nno\t: "<<no << "\nname\t: "<<name << "\n bal\t: "<< bal;
    }
    void main()
    {
    clrscr();
    bank b;// creating an object for bank;
    while(1)
    {
    cout << "\n1.Add a new account
    2.Deposit\n
    3.Withdraw\n
    4.View account detail\n
    5.exit\ ";

    int cas;
    cin >> cas;// to get the options 1/2/3/4/5;
    switch(cas)
    {
    case 1: //1.Add a new account
    clrscr();
    b.getdata();//to get datas from kwy board
    b.wwrite();//to write inside a file
    break;
    case 2: //2.Deposit
    case 3: //3.Withdraw
    case 4: // 4.View account detail";
    clrscr();
    b.rread() ;
    b.putdata();
    break;
    case 5: exit(0);
    }
    }
    getch();
    }
    // the problem is using wwrite() iam able to write inside the file. (not sure). but iam not able to retrive it using the rread() function. please find a solution for me. thanks.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    >>fs.write((char *)&this,sizeof(this));

    should be something like fs.write((char *)this,sizeof(*this));

    >>fs.read((char *)br,sizeof(br));

    What is br?

    You've also defined bank::putdata() twice.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM