std::copy question [modified]
-
#include <algorithm>
#include <fstream>
#include <sstream>int main()
{
std::fstream fs("d:/test.txt");
std::stringstream ss;
ss << "hello" << std::endl;
std::copy(std::istream_iterator<char>(ss),
std::istream_iterator<char>(),
std::ostream_iterator<char>(fs));
fs.close();
return 0;
}The code not work, I have some questions: 1. What does this mean: std::istream_iterator() ? 2. Is the position of the stream iterator depent on the current postion of the stream itselft? Thanks. 3. How to make the code work?
modified on Wednesday, November 4, 2009 9:13 AM
-
#include <algorithm>
#include <fstream>
#include <sstream>int main()
{
std::fstream fs("d:/test.txt");
std::stringstream ss;
ss << "hello" << std::endl;
std::copy(std::istream_iterator<char>(ss),
std::istream_iterator<char>(),
std::ostream_iterator<char>(fs));
fs.close();
return 0;
}The code not work, I have some questions: 1. What does this mean: std::istream_iterator() ? 2. Is the position of the stream iterator depent on the current postion of the stream itselft? Thanks. 3. How to make the code work?
modified on Wednesday, November 4, 2009 9:13 AM
Firstly - a CP/HTML thing - you need to escape the < characters in the template parameter spec of the iterators (looking at the source of the page, they should be
<char>
, yes?). Try declaringfs
as astd::ofstream
or making sure that the destination file exists - with that change, the code works fine.std::istream_iterator<char>()
is a sentinel value that represents the end of any input stream. Once anistream_iterator<char>
steps past the end of the file, it has the same value asstd::istream_iterator<char>()
. A stream iterator interacts with the stream position. It's a bad idea to manipulate the stream while there are active stream iterators.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Firstly - a CP/HTML thing - you need to escape the < characters in the template parameter spec of the iterators (looking at the source of the page, they should be
<char>
, yes?). Try declaringfs
as astd::ofstream
or making sure that the destination file exists - with that change, the code works fine.std::istream_iterator<char>()
is a sentinel value that represents the end of any input stream. Once anistream_iterator<char>
steps past the end of the file, it has the same value asstd::istream_iterator<char>()
. A stream iterator interacts with the stream position. It's a bad idea to manipulate the stream while there are active stream iterators.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p