0
我沒有從這段代碼輸出,可能是由於無限循環(不要聽我說)。我非常密切地關注着我的書,但無濟於事。fstream I/O不能讀取/寫入
我沒有得到任何錯誤,但是我跑步時沒有任何反應。
該程序需要打開一個文件,逐個字符地更改它的內容,並將它們寫入不同的文件(這是一個刪節版本)。
class FileFilter
{
protected:
ofstream newFile;
ifstream myFile;
char ch;
public:
void doFilter(ifstream &myFile, ostream &newFile)
{
while (ch!= EOF)
{
myFile.get(ch);
this->transform(ch);
newFile.put(ch);
virtual char transform (char)
{
return 'x';
}
};
class Upper : public FileFilter
{
public:
char transform(char ch)
{
ch = toupper(ch);
return ch;
}
};
int main()
{
ifstream myFile;
ofstream newFile;
myFile.open("test.txt");
newFile.open("new.txt");
Upper u;
FileFilter *f1 = &u;
if (myFile.is_open())
{
while (!nyFile.eof())
{
f1->doFilter(myFile, newFile);
}
}
else
{
cout << "warning";
}
myFile.close();
return 0;
}
是nyFile一個錯字? –
看起來你在FileFilter類中也缺少一對大括號。 –
我想知道爲什麼你需要在過濾器函數之外的while循環,因爲你在過濾器函數內部有while循環,反之亦然。 –