Blanc a déjà @ Andrew indiqué comment résoudre le problème que vous avez vu. Je vais jeter dans mon idée (en général pour moi, probablement trop d'ingénierie) idée de la façon de faire le reste du travail:
#pragma once
#if !defined(SENTINEL_ITERATOR_H_)
#define SENTINEL_ITERATOR_H_
#include <istream>
#include <iterator>
template <class T,
class charT=char,
class traits=std::char_traits<charT>,
class distance = ptrdiff_t>
class sentinel_iterator :
public std::iterator<std::input_iterator_tag,distance,void,void,void>
{
std::basic_istream<charT,traits> *is;
T value;
public:
typedef charT char_type;
typedef traits traits_type;
typedef std::basic_istream<charT,traits> istream_type;
sentinel_iterator(istream_type& s)
: is(&s)
{ s >> value; }
sentinel_iterator(T const &s) : is(0), value(s) { }
const T &operator*() const { return value; }
const T *operator->() const { return &value; }
sentinel_iterator &operator++() {
(*is)>>value;
return *this;
}
sentinel_iterator &operator++(int) {
sentinel_iterator tmp = *this;
(*is)>>value;
return (tmp);
}
bool operator==(sentinel_iterator<T,charT,traits,distance> const &x) {
return value == x.value;
}
bool operator!=(sentinel_iterator<T,charT,traits,distance> const &x) {
return !(value == x.value);
}
};
#endif
Ensuite, le code devient quelque chose comme ceci:
#include <algorithm>
#include <fstream>
#include "sentinel_iterator.h"
int main() {
ifstream infile("in.txt");
ofstream outfile("out.txt");
infile >> noskipws;
std::copy(sentinel_iterator<char>(infile),
sentinel_iterator<char>('?'),
std::ostream_iterator<char>(outfile));
return 0;
}
Merci @Andrew cela fonctionne. J'ai une question => le deafault beahvior de lire un fichier est "sauter les espaces" s'il y en a? – Alok
@Alok: ne pas oublier d'accepter la réponse en cliquant sur le chèque vert et donnant un bon upvote :) –
il montre l'orange pas vert:) - – Alok