fstream 객체를 전달하고 해당 토큰을 토큰 화합니다. 항상 hasNextToken()
에 파일을 열 수 없습니다. 누군가 나를 도울 수 있기를 바랍니다.fstream 파일을 읽을 수 없습니다.
//main.cpp
int main() {
string filename = "input.txt";
fstream inputStream(filename);
Tokenizer t(inputStream);
while (t.hasNextToken()) {
cout << t.nextToken();
}
}
//Tokenizer.h
class Tokenizer {
fstream fin;
public:
Tokenizer(fstream& file)
{
fin << file;
}
bool hasNextToken() {
if (!fin) {
cout << "Could not open file: " << endl;
exit(0);
}
return true;
}
string nextToken() {
string line;
getline(fin, line);
if (fin) {
istringstream sin(line);
string word;
sin >> word;
return word;
}
}
};
오류 메시지? –
'fin'은 결코 초기화되지 않습니다. –
이니셜 라이저 목록에 추가했지만 ios에 선언 된 비공개 멤버에 액세스하는 중 오류가 발생합니다. – newbieLinuxCpp