문자열로 변환 된 전화 조회 프로그램을 파일로 스트리밍하려고 시도 중입니다. 뭔가 빠졌어.하지만 막혔어. 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