File Handeling Problem In C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankaj255143
    New Member
    • Oct 2007
    • 17

    File Handeling Problem In C++

    i cannot get the output by using the function showdata()..... ...plz run this code and send the solution on my ID <ID removed by weaknessforcats >


    // A PROGRAME TO OPERATE SOME FILE RELATED OPERATIONS....

    #include<iostre am.h>
    #include<fstrea m.h>
    #include<conio. h>
    class stock
    {
    public:
    int no;
    char mfd[10]; // DD/MM/YYYY
    char exp[10]; // DD/MM/YYYY total 10 digits

    };
    void get()
    {
    clrscr();
    stock stk;
    cout<<"\n enter serial no : " ;
    cin>>stk.no;
    cout<<"\n enter mfd in DD:MM:YYYY formate : ";
    cin>>stk.mfd;
    cout<<"\n enter date of exp : ";
    cin>>stk.exp;
    ofstream fout;
    fout.open("d:/stock.txt",ios: :binary);
    fout.seekp(ios: :end);
    fout.write((cha r*) &stk ,sizeof stk);
    fout.close();
    }
    void showdata()
    {
    ifstream fin;
    stock stk;
    fin.open("d:/stock.txt",ios: :binary);
    fin.seekg(ios:: beg);
    fin.read((char *) &stk,sizeof stk);
    cout<<"serial : "<<stk.no;
    cout<<"\n mfd : "<<stk.mfd;
    cout<<"\n exp : "<<stk.exp;
    }


    void main()
    {
    stock stk;
    void get(void);
    void showdata(void);
    int choice;
    cout<<"\n enter your choice :\n enter 1 for input and 2 for show data : ";
    cin>>choice;
    switch(choice)
    {
    case 1:
    get();
    break;
    case 2:
    showdata();
    break;
    }
    getch();
    }
  • keerthiramanarayan
    New Member
    • Nov 2007
    • 13

    #2
    First of all i would ask you to see the Posting Guidelines.

    Coming to the solution, do you really require a class for something that holds only data. You would be better off storing the data as a structure. Next, to support portability and easy retrieval, it would be better if you wrote each member separately into the file. The runtime may arrange each type differently and cause problems. Try writing(reading ) each field separately and see the output.

    Comment

    • pankaj255143
      New Member
      • Oct 2007
      • 17

      #3
      Originally posted by keerthiramanara yan
      First of all i would ask you to see the Posting Guidelines.

      Coming to the solution, do you really require a class for something that holds only data. You would be better off storing the data as a structure. Next, to support portability and easy retrieval, it would be better if you wrote each member separately into the file. The runtime may arrange each type differently and cause problems. Try writing(reading ) each field separately and see the output.








      hello sir....thanks for giving reply..... i've assigned a project and this code is a part of that project(stock management system). i want to store the whole data in file through the object.bcz by using read and write functions i can easily retrive and save data to the file.......
      so plz try to understand my problem and give the solution.

      Comment

      Working...