入力イテレータと出力イテレータを取り、完了するまで入力ストリームを読み取る関数を作成する必要があります (私の最初の考えでは、入力ストリーム イテレータは読み取るファイルになるので、ファイルの内容が完全に読み取られると終了します)。戻ります。
この単純な実験では、テキスト ファイルから読み取られた文字は、単純に標準出力に出力されます。
私の問題は、次の方法がわからないことです。
A) 入力を繰り返す方法
B) イテレータが入力の最後に達したときを確認します。
C) エラーが発生した場合。
これまでの私のコードは次のとおりです。
#include <iostream>
#include <fstream>
#include <iterator>
bool readTo(std::istream_iterator<char> iit, std::ostream_iterator<char> oit)
{
bool ret(false);
// how to check when end of input file?
// how to iterate through input
// how to check if an error?
char ch = *iit;
*oit++ = 'c';
*oit++ = '=';
*oit++ = ch;
*oit++ = '\n';
return ret;
}
int main() {
std::ifstream strm("test.txt");
if(strm.good()) {
bool ret = readTo(strm, std::cout);
//I only want function to return when readTo read till end of input file
}
return 0;
}