【发布时间】:2014-05-12 18:14:34
【问题描述】:
我尝试在 C++ 中附加文件。在开始文件不存在。操作后,文件中只有一行而不是五行(此方法的 5 次调用)。看起来文件正在创建,接下来每个写操作文件都被清除并添加新字符串。
void storeUIDL(char *uidl) {
fstream uidlFile(uidlFilename, fstream::app | fstream::ate);
if (uidlFile.is_open()) {
uidlFile << uidl;
uidlFile.close();
} else {
cout << "Cannot open file";
}
}
我试过fstream::in ,fstream::out。如何在此文件中正确附加字符串?
提前谢谢你。
编辑:
这是更广泛的观点:
for (int i = 0; i < items; i++) {
MailInfo info = mails[i];
cout << "Downloading UIDL for email " << info.index << endl;
char *uidl = new char[100];
memset(uidl, 0, 100);
uidl = servicePOP3.UIDL(info.index);
if (uidl != NULL) {
if (existsUIDL(uidl) == false) {
cout << "Downloading mail with index " << info.index << endl;
char *content = servicePOP3.RETR(info);
/// save mail to file
string filename = string("mail_" + string(uidl) + ".eml");
saveBufferToFile(content, filename.c_str());
storeUIDL(uidl);
sleep(1);
} else {
cout << "Mail already exists." << endl;
}
} else {
cout << "UIDL for email " << info.index << " does not exists";
}
memset(uidl, 0, 100);
sleep(1);
}
【问题讨论】:
-
尝试删除
fstream::ate。 -
试试这个:
fstream uidlFile; uidlFile.open(uidlFilename, fstream::in | fstream::out | fstream::app); -
这个文件中仍然只存储最后一行。
-
是的。这很奇怪,但不起作用……请查看已编辑的问题。添加代码。