473,715 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostream_iterato r from fstream

a std::ostream_it erator 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_iterato r?
I am not using a pointer to any base class.

Fraser.
Jul 22 '05 #1
12 2494

"Fraser Ross" <fraserATmember s.v21.co.united kingdom> wrote in message
news:40******** @news.greennet. net...
a std::ostream_it erator 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_iterato r? I am not using a pointer to any base class.

Fraser.


fstream derives from ostream, so you can construct an ostream_iterato r 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_iterato r. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::ostream_i terator<T,charT ,traits>::ostre am_iterator(std ::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_i terator<T,charT ,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<Inpu tIterator,Outpu tIterator>(char *,char *,undefined)'.
Fraser.
Jul 22 '05 #3

"Fraser Ross" <fraserATmember s.v21.co.united kingdom> wrote in message
news:40******@n ews.greennet.ne t...
I am trying to use std::copy outputing to ostream_iterato r. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::ostream_i terator<T,charT ,traits>::ostre am_iterator(std ::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_i terator<T,charT ,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<Inpu tIterator,Outpu tIterator>(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_iterato r<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
<fraserATmember s.v21.co.united kingdom@?.?.inv alid> writes
I am trying to use std::copy outputing to ostream_iterato r. I use BCB4.
The messages are:
[C++ Error] (3): E2285 Could not find a match for
'std::ostream_ iterator<T,char T,traits>::ostr eam_iterator(st d::fstream)'.
[C++ Error] (3): E2031 Cannot cast from 'std::fstream' to
'std::ostream_ iterator<T,char T,traits>'.
[C++ Error] (3): E2285 Could not find a match for
'std::copy<Inp utIterator,Outp utIterator>(cha r *,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" <fraserATmember s.v21.co.united kingdom> wrote in message
news:40******@n ews.greennet.ne t...

"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
<fraserATmember s.v21.co.united kingdom@?.?.inv alid> 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_iterato r<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
<fraserATmember s.v21.co.united kingdom> wrote:

"John Harrison"
copy(msg, msg + strlen(msg), ostream_iterato r<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_iterato r you want. Just as
you would with any other template class.

john
Jul 22 '05 #10

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

Similar topics

2
2038
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
4615
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 non-zero number follows. ---------------- error message --------------------
5
2550
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 first element of int_list. after the first element is copied to cout the stream setw(3) no longer has any affect.
4
1993
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
939
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 from http://www.camtp.uni-mb.si/books/Thinking-in-C++/Chapter04.html : //: C04:Intset.cpp // Simple use of STL set #include <set>
5
3656
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 define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
2
7280
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
1380
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 compiler error, and I'm stymied as to why. It's seems to be related to the string type, because iterating over pairs of other types has worked fine for me. The (rather verbose) error is: /usr/include/c++/4.2/bits/stream_iterator.h:196: error: no...
20
3259
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 NewShapes.cpp:12: error: template argument 1 is invalid NewShapes.cpp:12: error: template argument 2 is invalid" I may change all code but not change the code in main (sorry), I may add code to the main but not change it. NewShapes.cpp:
0
8821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.