ostream_iterator from fstream

F

Fraser Ross

a std::eek:stream_iterator can be constructed from a ostream but not a fstream.
If the fstream is opened with the out mode how can I get a ostream_iterator?
I am not using a pointer to any base class.

Fraser.
 
J

John Harrison

Fraser Ross said:
a std::eek:stream_iterator can be constructed from a ostream but not a fstream.
If the fstream is opened with the out mode how can I get a ostream_iterator?
I am not using a pointer to any base class.

Fraser.

fstream derives from ostream, so you can construct an ostream_iterator from
an fstream.

Do you have some code where it's not working?

john
 
F

Fraser Ross

I am trying to use std::copy outputing to ostream_iterator. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::eek:stream_iterator<T,charT,traits>::eek:stream_iterator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::eek:stream_iterator<T,charT,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<InputIterator,OutputIterator>(char *,char *,undefined)'.


Fraser.
 
J

John Harrison

Fraser Ross said:
I am trying to use std::copy outputing to ostream_iterator. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::eek:stream_iterator<T,charT,traits>::eek:stream_iterator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::eek:stream_iterator<T,charT,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<InputIterator,OutputIterator>(char *,char *,undefined)'.

Would have helped to include that code that gives those error messages.

The following compiles for me even on the ancient VC++ 6, it also compiles
on Comeau C++.

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;

int main(int argc, char** argv)
{
fstream file("some_file.txt");
const char msg[] = "something or other";
copy(msg, msg + strlen(msg), ostream_iterator<char>(file, ""));
}

As I said this should work, so either you have a duff compiler, a duff STL
implementation or you've made a mistake in your code.

john
 
R

Richard Herring

Fraser Ross said:
I am trying to use std::copy outputing to ostream_iterator. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::eek:stream_iterator<T,charT,traits>::eek:stream_iterator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::eek:stream_iterator<T,charT,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<InputIterator,OutputIterator>(char *,char *,undefined)'.
And the corresponding C++ code is?
 
J

John Harrison

Fraser Ross said:
"Richard Herring"


You can imagine what it is. It is only common instructions.

I would still suggest you post it. Its very easy to make mistakes without
realising that you have. At least we eliminate one possibility and then we
can start thinking about workarounds.

john
 
R

Richard Herring

Fraser Ross said:
"Richard Herring"


You can imagine what it is. It is only common instructions.

Fine. In that case you can imagine my helpful response telling you where
your error is. It is only common words.
 
F

Fraser Ross

"John Harrison"
copy(msg, msg + strlen(msg), ostream_iterator<char>(file, ""));

The explicit specialisation is what my code needed. The second parameter is
unnecessary.

I notice that you can use pointers/iterators to a range of constant
elements. I had to make the elements non-const. I often have to do this.

Fraser.
 
J

John Harrison

"John Harrison"

The explicit specialisation is what my code needed. The second
parameter is
unnecessary.

I notice that you can use pointers/iterators to a range of constant
elements. I had to make the elements non-const. I often have to do
this.

Fraser.

Yes you have to specify what type of ostream_iterator you want. Just as
you would with any other template class.

john
 
F

Fraser Ross

"John Harrison"
Yes you have to specify what type of ostream_iterator you want. Just as
you would with any other template class.

Sometimes the template parameter/s can be deduced. I thought it would be
with ostream_iterator.

Fraser.
 
J

John Harrison

"John Harrison"

Sometimes the template parameter/s can be deduced. I thought it would be
with ostream_iterator.

Template parameter deduction only happens for function templates,
ostream_iterator is a class template.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top