gpt4 book ai didi

c++ - 将数组打印到文本文件中

转载 作者:行者123 更新时间:2023-11-28 05:55:22 30 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c++ - 将数组打印到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34251034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com