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

In c++ ofstream and vector write blank file

当我 运行 我的代码时,下面的矢量存储了正确的数据,但由于某种原因它没有正确写入硬编码文件路径,而是将 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

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

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

randomVector有数据吗?因为如果不这样做,您的文件将保持为空,因为不会写入任何内容。

还有,如果你没有任何数据,真的有必要打开那个文件进行写入吗?我会先检查向量是否有任何数据,例如:

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

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

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

--- 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。您使用的 OS 和编译器是什么?