473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

ostream_iterator from fstream

a std::ostream_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.
Jul 22 '05 #1
12 2465

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:40********@news.greennet.net...
a std::ostream_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
Jul 22 '05 #2
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::ostream_iterator<T,charT,traits>::ostream_it erator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_iterator<T,charT,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<InputIterator,OutputIterator>(char *,char *,undefined)'.
Fraser.
Jul 22 '05 #3

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:40******@news.greennet.net...
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::ostream_iterator<T,charT,traits>::ostream_it erator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_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
Jul 22 '05 #4
In message <40******@news.greennet.net>, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom@?.?.invali d> writes
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::ostream_iterator<T,charT,traits>::ostream_i terator(std::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_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?

--
Richard Herring
Jul 22 '05 #5

"Richard Herring"
And the corresponding C++ code is?

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

Fraser.
Jul 22 '05 #6

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:40******@news.greennet.net...

"Richard Herring"
And the corresponding C++ code is?

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
Jul 22 '05 #7
In message <40******@news.greennet.net>, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom@?.?.invali d> writes

"Richard Herring"
And the corresponding C++ code is?

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.

--
Richard Herring
Jul 22 '05 #8

"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.
Jul 22 '05 #9
On Tue, 13 Jul 2004 16:42:38 +0100, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom> wrote:

"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.


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

john
Jul 22 '05 #10

"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.
Jul 22 '05 #11
On Tue, 13 Jul 2004 17:51:29 +0100, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom> wrote:

"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.


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

john
Jul 22 '05 #12
On Tue, 13 Jul 2004 15:44:14 +0100, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> wrote:

"Richard Herring"
And the corresponding C++ code is?

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


Not it isn't, since you've made a mistake in the "instructions".

Tom
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Mantoulidis | last post by:
Hello all... I get an error message for the following program by the compiler: #include <iostream> #include <string> using namespace std; ostream_iterator<string> oo(cout);
8
by: Jeff | last post by:
std::copy to std::ostream_iterator doesn't seem to work with maps and pairs. Shouldn't I expect something like this to work? The G++ error message when USE_COPY_TO_PRINT is defined as a...
5
by: Wiseguy | last post by:
consider the following: ostream_iterator<int> i(cout,""); cout << setw(3); copy(int_list.begin(),int_list.end(),i); the problem is that the field width restriction is only active for the...
4
by: Nan Li | last post by:
Hello, Can any one get the 'copy' statement below to work? I want it to do the same thing as the 'for' loop does. But I got a lot of STL error during compile. Thanks, Nan #include <map>
3
by: Evyn | last post by:
Hi, Can someone tell me why I always get an error like "`ostream_iterator' undeclared (first use this function) " when using ostream_iterator. I have for example tried the following code taken...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
0
by: ShaunJ | last post by:
I'm trying to use an ostream_iterator to iterate over a set of pairs. I've used an ostream_iterator over other types and had no problem. However, with the following code snippet, I'm getting a...
20
by: Adalte | last post by:
Hi ! When I compile this piece of code I get the following error: "NewShapes.cpp: In function `int main()': NewShapes.cpp:12: error: `ShapePtr' cannot appear in a constant-expression...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.