将数组打印到textfile中

时间:2015-12-13 12:24:19

标签: c++ arrays ostream

我目前正在尝试将文件的内容打印到文本文件中,但出来的数据只是一个地址。如果有人能给我建议,我将非常感激。

ostream& operator<<(ostream& out, const BusinessContact& Con)
{
  out << Con.firstName << " "
  << Con.lastName << " "
  << Con.phoneNumber<< " "
  << Con.emailAddress<< " "
  << Con.company << " "<<endl;
  return out;
}

BusinessContact* Alfred = new BusinessContact("Alfred", "Butler", "999-999-9999", "Alfred@Wayne.ent", "Gotham");
BusinessContact* Bruce  = new BusinessContact("Bruce", "Wayne", "999-999-0000", "Batman@Wayne.ent", "Gotham");
BusinessContact* Clark  = new BusinessContact("Clark", "Kent", "888-888-8888", "Superman@Kryponyte.wrd", "Metropolis");
BusinessContact* Luther = new BusinessContact ("Lex", "Luther", "888-888-1313", "Evil@Evil.Ent", "Metropolis");

array<BusinessContact*, 10> listBContacts{Alfred, Bruce, Clark, Luther};

void arrayTransform()
{
try
{
    cout << "Write Contacts to file..." << endl;
    ofstream OUT("Contacts.txt", ios::out);
    if (!OUT)
        throw new string("Contacts.txt not opened... ");
        for (int i = 0; i < listBContacts.size(); ++i)
        {
            OUT << listBContacts[i];
        }   OUT.close();
            system("pause");
} catch(string*msg)
    {
        cerr<< "Exception: " << *msg << endl;
    }
}

1 个答案:

答案 0 :(得分:1)

数组listBContacts包含指针,而不是值。您需要首先取消引用指针才能打印它们:OUT << *(listBContacts[i]);