So I wanted to echo a file into standard output through the use of stream iterators, but I encountered an unexcepted output when I used strings compared to characters. Here's my code with chars (works properly):
Code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <string>
#include <stdexcept>
#include <iomanip>
int main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "Incorrent arguments." << std::endl;
throw std::runtime_error("");
}
std::ifstream inFile(argv[1]);
inFile >> std::noskipws;
if (!inFile) {
std::cerr << "Error opening file." << std::endl;
throw std::runtime_error("");
}
std::istream_iterator<char> iterIn(inFile);
std::istream_iterator<char> iterEOF;
std::ostream_iterator<char> iterOut(std::cout);
std::copy(iterIn, iterEOF, iterOut);
return 0;
}
The only difference between this one and my "string" version is this:
Code:
std::istream_iterator<std::string> iterIn(inFile);
std::istream_iterator<std::string> iterEOF;
std::ostream_iterator<std::string> iterOut(std::cout);
The output I get from the string version is that I only get the "first word" and the program quits. If I omit the noskipws then it works properly but with no spaces so the output isn't exactly as I wanted it. Why doesn't the string one work like the char version is my primary question? Any pointers here would be good.
P.S Learning C++ makes C feel so much easier - too much to learn!