Reading a class object from ASCII file...

This is a discussion on Reading a class object from ASCII file... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> // cin, cout #include <fstream> // ifstream, ofstream #include <cstring> // strcpy, strlen, strncmp #include <cctype> // ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Reading a class object from ASCII file...

    Code:
    #include  <iostream>         //  cin, cout
    #include  <fstream>          //  ifstream, ofstream
    #include  <cstring>          //  strcpy, strlen, strncmp
    #include  <cctype>           //  toupper
    #include  <cstdlib>          //  itoa
    using namespace std;
    
    class Person
    {
    private:
        int nr;
        char* Name;
    public:
        Person()
        {
            Name = "Testdrive";
            nr = 123456;
        }
    
        Person(int i)
        {
            Name = "ABCDEF";
            nr = i;
        }
    
        int getnr()
        {
            return nr;
        }
        char* getname()
        {
            return Name;
        }
    };
    
    //GLOBAL VARIABLES
    Person* p = new Person();
    Person* p2 = new Person(124124);
    
    //GLOBAL FUNCTIONS
    void ReadFile()
    {
        int i;
        char* c;
    
        cout << endl << endl << " READING FILE " << endl;
    
        ifstream innfil("person.dta");
        while (innfil.eof() > 0) {                                 If I change this one innfil.eof, something bad is happening.
             innfil >> i >> c;                                         //IF I let it be, it just skips the code within this while-loop. 
            cout << endl << " from read file " << i 
                    << " name from read " <<c << endl;
        }
    }
    
    
    void WriteFile()
    {
        ofstream out("person.dta");
        cout << p->getnr() << p->getname();
        out << p->getnr()<< " " << p->getname();
    
        cout << p2->getnr() << p2->getname();
        //out << p2->getnr()<< " " << p2->getname();
    
        out.close();
    }
    
    //MAIN PROGRAMME
    int main()
    {
        WriteFile();
    
        ReadFile();
        cout << "done";
        char c;
        cin >> c;
    }
    Edited code:
    Edit: Wondering how to solve this, seriously... Can someone show me an example, fix mine?
    Last edited by ManyTimes; 03-20-2010 at 09:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to parse the input yourself (you don't get an input mechanism for free, just by declaring the class). In particular, if you expect there to be "user-friendly" things in your file (like "Number:" and "Name:"), then your input function will need to be ready to use them/ignore them.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Jesus holy...

    I needed a "innfil.ignore()"; It solved everything, I see now, it doesnt know when to "jump to the next line", unless i tell it to do so...
    Last edited by ManyTimes; 03-20-2010 at 09:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21