在c ++ ofstream和vector中写入空白文件

时间:2015-09-23 15:44:08

标签: c++ vector ofstream

当我运行我的代码时,下面的向量中存储了正确的数据,但由于某种原因,它无法正确写入硬编码文件路径,而是将txt文件留空。我确信这是一件很简单的事情,我忽视了。

更新:这是更完整的代码。

// 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

帐户类

// 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();
}

人类

#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;
}

2 个答案:

答案 0 :(得分:0)

randomVector是否有任何数据?因为如果不是,你的文件将保持为空,因为什么都不会被写入。

另外,如果您没有任何数据,是否真的有必要打开该文件进行写作?我会首先检查向量是否有任何数据,例如:

if(!randomVector.empty())
{ 
    // Your code here!
}

我不知道getStuff()getMoreStuff()会返回什么,但至少你应该在文件中看到已定义的空白,但前提是你的向量有一些元素。

答案 1 :(得分:0)

如果我将这些更改应用于您的代码(“ - ”是删除的行,则添加“+”)

--- 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;

我最终得到的accountData.txt如下所示:

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

所以修改后的代码似乎没问题。这是在Ubuntu 14.04,编译器g ++ 4.8.4。您使用的操作系统和编译器是什么?