Search code examples
c++vectorofstream

In c++ ofstream and vector write blank file


When I run my code the vector below has the correct data stored in it, but for some reason it does not write correctly to the hard-coded file path, it instead leaves the txt file blank. I'm sure it is something simple I'm overlooking.

UPDATED: Here is more complete code.

// Declare the necessary include(s)
#include <iostream>
#include <vector>
#include "Account.h"

/* main function
Purpose: To test the creation of two classes & make sure they output correctly
Parameters: None
Returns: an int (0) */
int main()
{
    // Declare local variable
    int i = 0;

    // Create a vector of Account objects.
    vector <Random> randomVector;

    // Create three person objects.
    Person bob("billy bob", "bobsway");
    Person joe("joe joe", "cityofjoe");
    Person george("george jack", "georgetown");

    // Create three Account objects, where each account object contains a Person object.
    Account bobby(bob, 1, 500);
    Account joseph(joe, 2, 1000);
    Account george(george, 3, 1200);

    // Push these Account objects into the vector.
    randomVector.push_back(bobby);
    randomVector.push_back(joseph);
    randomVector.push_back(george);

    // Create an ofstream object
    ofstream oDataAccount("accountData.txt");

    // Create a loop to write Account data to a file
    for (i = 0; i < randomVector.size(); i++)
    {
        // Flush the output file
        oDataAccount.flush();

        // Write the data from each object in the vector
        randomVector[i].writeData(oDataAccount);
    }

    // Close the file.
    oDataAccount.close();

// Keep the console window open with PAUSE
    system("PAUSE");

    // Return a 0
    return 0;

}// End main

Account Class

// Include pragma once
#pragma once
#include "Person.h"

class Account
{

private:

    Person aPerson;
    int accountNum;
    double accountBalance;

public:

    Account();

    Account(const Person, int, double);

    Person getPerson();

    int getAccountNum();

    double getAccountBalance();

    // writeData function
    void writeData(ofstream&);

};

// Include the Account header file
#include "Account.h"

Account::Account()
{
    // Initializes data members
}

Account::Account(const Person p, int accNum, double accBal)
{
    aPerson = p;
    accountNum = accNum;
    accountBalance = accBal;
}

Person Account::getPerson()
{
    return aPerson;
}

int Account::getAccountNum()
{
    return accountNum;
}

double Account::getAccountBalance()
{
    return accountBalance;
}

// Implementation for writeData function
void Account::writeData(ofstream& output)
{
    // Write class data to the file
    output << getPerson().getName() << ' ' << getPerson().getAddress() << ' ' << getAccountNum() << ' ' << getAccountBalance();
}

Person Class

#pragma once
#include <string>
#include <fstream>
using namespace std;

class Person
{

private:

    string name;
    string address;

public:

    Person();

    Person(const string, const string);

    string getName();

    string getAddress();

};

#include "Person.h"

Person::Person()
{
}

Person::Person(const string n, const string a)
{
    name = n;
    address = a;
}

string Person::getName()
{
    // Return the name
    return name;
}

string Person::getAddress()
{
    // Return the address
    return address;
}

Solution

  • If I apply these changes to your code (the "-" are removed lines, the "+" are added)

    --- a/main.cpp
    +++ b/main.cpp
    @@ -13,7 +13,7 @@ int main()
         int i = 0;
    
         // Create a vector of Account objects.
    -    vector <Random> randomVector;
    +    vector <Account> randomVector;
    
         // Create three person objects.
         Person bob("billy bob", "bobsway");
    @@ -23,12 +23,12 @@ int main()
         // Create three Account objects, where each account object contains a Person object.
         Account bobby(bob, 1, 500);
         Account joseph(joe, 2, 1000);
    -    Account george(george, 3, 1200);
    +    Account georgy(george, 3, 1200);
    
         // Push these Account objects into the vector.
         randomVector.push_back(bobby);
         randomVector.push_back(joseph);
    -    randomVector.push_back(george);
    +    randomVector.push_back(georgy);
    
         // Create an ofstream object
         ofstream oDataAccount("accountData.txt");
    @@ -47,7 +47,7 @@ int main()
         oDataAccount.close();
    
     // Keep the console window open with PAUSE
    -    system("PAUSE");
    +    //system("PAUSE");
    
         // Return a 0
         return 0;
    

    I end up with an accountData.txt that looks like this:

    billy bob bobsway 1 500
    joe joe cityofjoe 2 1000
    george jack georgetown 3 1200
    

    So the modified code seems OK. This is on Ubuntu 14.04, compiler g++ 4.8.4. What OS and compiler are you using?