[英]Print array into textfile
我目前正在嘗試將文件的內容打印到文本文件中,但是輸出的數據只是一個地址。 如果有人可以給我建議,將不勝感激。
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]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.