我想將字符串流的手機查找程序轉換成文件流..我錯過了一些東西,但我卡住了..什麼成員我可以在ofstream過程中得到這工作?C++從文件讀取向量
ofstream& process (ofstream &os, vector<PersonInfo> people)
{
// for each entry in people
for (vector<PersonInfo>::const_iterator entry = people.begin();
entry != people.end(); ++entry) {
ofstream formatted, badNums; // objects created on each loop
// for each number
for (vector<string>::const_iterator nums = entry->phones.begin();
nums != entry->phones.end(); ++nums) {
if (!valid(*nums)) {
badNums << " " << *nums; // string in badNums
} else
// ``writes'' to formatted's string
formatted << " " << format(*nums);
}
if (badNums.empty()) // there were no bad numbers
os << entry->name << " " // print the name
<< formatted.str() << endl; // and reformatted numbers
else // otherwise, print the name and bad numbers
cerr << "input error: " << entry->name
<< " invalid number(s) " << badNums.str() << endl;
}
return os;
}
你沒有將你的流與文件關聯起來。您需要調用'open()'並傳遞文件名(或使用適當的構造函數)。並且流沒有名爲'empty()'的成員函數。 – jrok