Thread: file IO only writing for 1 element

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    file IO only writing for 1 element

    I have a vector of students and im trying to write them into a txt file. However, it will only write 1 student only. I also am not really sure on how to implement read file also. I got something in my mind but I;m not sure if it's correct or not.

    calling write to file function from main
    Code:
    for(vector<clsStudent*>::const_iterator it = student.begin(); it != student.end();++it)
    {
         (*it)->writeToFile();
    }


    Code:
    const char * filename = "studentinfo.txt";
    
    
    void clsLocalStudent::writeToFile()
    {
        clsUniversityProgram *ptrProgram;
        ptrProgram = getProgram();
    
    
       ofstream out(filename);
       if(out)
       {
           out << getName() << "\t"
                << getDateOfBirth() << "\t"
                << getGender() << "\t"
                << getNationality() << "\t"
                << getGPA() << "\t"
                << ptrProgram->getProgramName() << "\t"
                << ptrProgram->getProgramCode() << "\t"
                << ptrProgram->getProgramFees() << "\t"
                << getAccounts()->getAccountHolderName() << "\t"
                << getAccounts()->getAccountHolderID() << "\t"
                << getAccounts()->getAccountHolderDOB() << "\t"
                << getAccounts()->getAccountStartDate() << "\t"
                << getAccounts()->getPrincipal() << "\t"
                << getAccounts()->getYear() << "\t"
                << getAccounts()->getCI() << "\t"
                << getAccounts()->getC_Amount() << "\t"
                << calculateFees() << "\t"
                << getIC() << "\t"
                << getState();
          out.close();
       }
       else
          cerr << "\nCannot open " << filename << " for writing" << endl;
    }
    what i had in mind
    Code:
    void readFromFile(vector <clsStudent *> &s)
    {
    string name, double GPA;
       ifstream in(filename);
       if(in)
       {
          in >> name >> GPA;
          in.close();
       }
       else
          cerr << "\nCannot open " << filename << " for reading" << endl;
    
       student = new clsLocalStudent(name, GPA); // parameterized constructor
       s.push_back(student);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the first thing your write to file function does is delete all prior contents of the file. If you don't want that to happen, you should probably tack on ios::append in your constructor there.

    EDIT: Apparently I mean app instead of append.
    Last edited by tabstop; 12-01-2013 at 09:11 AM.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    this?
    Code:
    ofstream out(filename, ios::app);

    do i need to do anything to make it append at the end of file?
    Last edited by Alexius Lim; 12-01-2013 at 09:22 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think "if" stands for output file, do you?

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    59
    Quote Originally Posted by tabstop View Post
    I don't think "if" stands for output file, do you?
    Haha, my mistake. Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Decouple the opening of the file with the writing to the file. For example:
    Code:
    void clsLocalStudent::writeToFile(std::ostream& out) const
    {
        clsUniversityProgram* ptrProgram = getProgram();
    
        out << getName() << "\t"
            << getDateOfBirth() << "\t"
            << getGender() << "\t"
            << getNationality() << "\t"
            << getGPA() << "\t"
            << ptrProgram->getProgramName() << "\t"
            << ptrProgram->getProgramCode() << "\t"
            << ptrProgram->getProgramFees() << "\t"
            << getAccounts()->getAccountHolderName() << "\t"
            << getAccounts()->getAccountHolderID() << "\t"
            << getAccounts()->getAccountHolderDOB() << "\t"
            << getAccounts()->getAccountStartDate() << "\t"
            << getAccounts()->getPrincipal() << "\t"
            << getAccounts()->getYear() << "\t"
            << getAccounts()->getCI() << "\t"
            << getAccounts()->getC_Amount() << "\t"
            << calculateFees() << "\t"
            << getIC() << "\t"
            << getState();
    }
    
    // ...
    
    const char* filename = "studentinfo.txt";
    ofstream out(filename);
    if (out)
    {
        for (vector<clsStudent*>::const_iterator it = student.begin(); it != student.end(); ++it)
        {
            (*it)->writeToFile(out);
        }
    }
    else
    {
        cerr << "\nCannot open " << filename << " for writing" << endl;
    }
    In fact, for this purpose, it is common to overload operator<< to do what your writeToFile member function does.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting last element in file
    By sigur47 in forum C Programming
    Replies: 2
    Last Post: 12-14-2011, 06:29 AM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  3. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  4. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  5. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM

Tags for this Thread