2015-09-23 44 views
0

当我运行我的代码时,下面的向量具有存储在其中的正确数据,但由于某些原因,它没有正确写入硬编码文件路径,而是保留了txt文件空白。我相信这是我忽略的一些简单的东西。在C++ ofstream和vector中写入空白文件

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

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

Person类

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

FYI'ofstream oDataFile(“randomData.txt”);'已经打开文件,你不需要明确地打开' – CoryKramer

+0

你的向量可以是空的。发布完整的可编译示例。 –

+0

我想添加一个randomVector.size()不是0的检查;如果是,则该文件将为空白。这将有助于有一个完整的例子(我很欣赏这可能是不可能的)。 –

回答

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。你使用的是什么操作系统和编译器?

+0

我正在使用Windows和Visual Studio,但您的回答引导我朝着更好的方向发展。 – Tarquiniius

0

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

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

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

我不知道是什么getStuff()getMoreStuff()回报,但至少你应该看到定义空白文件中,但只有当你的载体有一些元素。

+0

我发布了一些更完整的代码。 &它不返回空,它有它应该的数据。 – Tarquiniius

+0

什么是“随机”?它是否与“Account”类相同?像你更新的代码不会编译,至少不会在我的系统上编译。你是如何定义“Random”的? –