将数组打印到文本文件中
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]);
我目前正在尝试将文件的内容打印到文本文件中,但输出的数据只是一个地址。如果有人能给我建议,我将不胜感激。
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]);