#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    CANT access stored data from file


    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    #include<string.h>
    #include<stdio.h>
    #include<process.h>
    int tempint1=0, gpno;
    fstream f1; ifstream f2; ofstream f3;
    void loadgpno(){
    start1:
    f2.open("gpno.txt");
    if(!f2){f3.open("gpno.txt");
    f3<<tempint1; f3.close(); goto start1;}
    else
    {f2.open("gpno.txt");f3.open("gpno.txt");f2>>gpno;f2.close();f3<<++gpno;f3.close();}
    }
    class paper
    {public:
    int pno,tot; float rate; char name[25], allias[10];
    void add()
    {loadgpno(); pno=gpno;tot=0;
    cout<<"\nEnter Name: ";gets(name); cout<<"Enter Allias: "; gets(allias);
    cout<<"Enter Rate: "; cin>>rate;}
    void show()
    {cout<<"\n"<<pno<<"\t"<<name<<"\t"<<allias<<"\t"<<rate<<"\t"<<tot;}

    }pob[20],pob1;

    void main()
    {clrscr(); int m,n;
    start2:
    cout<<"1. Add\n2. Load\n3. Show\n4. Exit\n"; cin>>n;
    if(n==1)
    {cout<<"Enter No of entries: ";cin>>m;
    f1.open("paper.dat",ios::out|ios::app|ios::binary|ios::nocreate);
    for(int i=0;i<m;i++){pob1.add(); f1.write((char*)&pob1,sizeof(pob1));} f1.close();}
    else if(n==2)
    { f1.open("paper.dat",ios::in|ios::binary);
    for(int i=0;i<gpno;i++)
    { f1.read((char*)&pob1,sizeof(pob1)); pob[i]=pob1;}
    f1.close(); }
    else if(n==3)
    {for(int i=0;i<gpno;i++)
    {pob[i].show();}
    }
    else if(n==4)
    {exit(0);}
    goto start2 ;

    }
    ********************************************************************************************
    I want to store data of class PAPER to paper.dat. and then access it from the file and store it to class PAPER till the program closes.
    Now I can store it but cant access it after reopening the program.

    PLEASE HELP
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Well it was nice that you used code tags in the other forum , maybe you could duplicate that effort here.

    Jim
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Absolutely no idea, because nobody could make any sense out of that unreadable mess made even worse by those stupid smilies.

    Use code tags! HTML strips out extra white space, obliterating the formatting of code listings. You need to use code tags to preserve that formatting. Fail to do that and you will never hear the end of it here.

    [code] insert your formatted code here [/code]

    Also, the forum software automatically interprets certain character sequences as smilies, character sequences that are very common in C++ code. Always disable smilies. In the advanced editor you will find a "Disable smilies" check box. Always check that box.

    Here is what your code looks like when the above is followed (original formatting retrieved via the Reply button):

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    #include<string.h>
    #include<stdio.h>
    #include<process.h>
    int tempint1=0, gpno;
    fstream f1; ifstream f2; ofstream f3;
    void loadgpno(){
    	 start1:
      f2.open("gpno.txt");
    	 if(!f2){f3.open("gpno.txt");
    	 f3<<tempint1; f3.close(); goto start1;}
    	 else
    	 {f2.open("gpno.txt");f3.open("gpno.txt");f2>>gpno;f2.close();f3<<++gpno;f3.close();}
      }
    class paper
    {public:
    int pno,tot; float rate; char name[25], allias[10];
    void add()
    {loadgpno();   pno=gpno;tot=0;
     cout<<"\nEnter Name: ";gets(name); cout<<"Enter Allias: "; gets(allias);
     cout<<"Enter Rate: "; cin>>rate;}
     void show()
     {cout<<"\n"<<pno<<"\t"<<name<<"\t"<<allias<<"\t"<<rate<<"\t"<<tot;}
    
    }pob[20],pob1;
    
    void main()
    {clrscr(); int m,n;
    start2:
    cout<<"1. Add\n2. Load\n3. Show\n4. Exit\n";    cin>>n;
    if(n==1)
    {cout<<"Enter No of entries: ";cin>>m;
      f1.open("paper.dat",ios::out|ios::app|ios::binary|ios::nocreate);
     for(int i=0;i<m;i++){pob1.add();   f1.write((char*)&pob1,sizeof(pob1));} f1.close();}
      else if(n==2)
     { f1.open("paper.dat",ios::in|ios::binary);
    for(int i=0;i<gpno;i++) 
    { f1.read((char*)&pob1,sizeof(pob1)); pob[i]=pob1;}
     f1.close(); }
     else if(n==3)
      {for(int i=0;i<gpno;i++)
    {pob[i].show();}
    }
    else if(n==4)
    {exit(0);}
     goto start2 ;
    
    }
    Jeez! It's still an unreadable mess! Crappy inconsistent indenting. Hiding close braces at the end of lines and ... wait a moment ... even in the middle of lines! Piling several statements into the same line, including statements that belong to other code blocks. You have got to be kidding us!

    Since you are so absolutely dead set against anybody being able to read your code, we'll let you have your way. We won't bother trying to read it. Aren't you happy that you got you way?

    Though I did spot one thing, labels. There's only one purpose I can think of for labels, which are goto's. So I had my browser do a search and it found six goto's in your code. goto??? Really? You are using goto? In C++? In a C++ program that uses classes? Who taught you such an idiotic practice? goto creates spaghetti code lacking structure. Spaghetti code is inherently unreadable. Boy, you are maniacal about making your code unreadable!

    Those goto's are a strong indicator that your code is fracked up. And since nobody can read it, nobody can answer your question.


    If you really do want help finding a solution to your problem, then you need to help us to help you! All you need to do to facilitate that is to clean up your code:

    1. Place all statements on separate lines. There are a few times when having more than one statement on a line makes the code more readable, but such times are rare and your program is more certainly not one.

    2. Indent your code consistently.

    3. Put the open and close braces ( { } ) where everybody can see them. I very strongly recommend the Allman style ( http://en.wikipedia.org/wiki/Indent_style#Allman_style) which places both open and close braces on their own lines and at the same level of indentation. It is very readable.

    Once, our new software supervisor presented us with coding style requirements that included using K&R style in which the open brace is hidden at the end of the preceding line. The entire programming team immediately objected as one and all for the same reason, because K&R is less readable than Allman style is. He immediately saw the error of his ways and repented.

    4. Insert an extra line here and there to improve readability. An obvious place would be between individual classes, functions, and method implementations.

    5. Remove every goto and replace them with control structures. Again, there are very rare cases where a goto can actually be useful. Your code is completely and absolutely devoid of any of those rare cases. You learned control structures as part of structured programming. I know that because that is how C and C++ are taught. There is a reason for teaching structured programming, because it is far superior to spaghetti code. Apply what you learned.

    6. Use code tags. Frankie Manning: "A 1, a 2, you know what to do."

    7. Always disable smilies. Unfortunately there is no User Profile option to do this permanently, so you need to do it for each and every message you post here. Sorry.


    Remember: until we can read your code, we cannot help you.

    Help us to help you.

IMN logo majestic logo threadwatch logo seochat tools logo