473,714 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream, copy algorithm .. more

There's no way to use the STL algorithm copy to print an outfile
(essentially an ofstream)? So now:

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::ofstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
// ToFile << InFile.rdbuf();

////////////////////////////
// Alternative 2
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( ToFile, "\n" ) );

// How do I print whats in the 'ToFile'??? Perhaps some other algo?

return 0;
}

Assume the contents of InFile are as follows:
1 2 3 4 5
4 5 6 7 8

The fact that ToFile reflects InFile is a good thing but I'd have
thought the call to ostream_iterato r with the '\n' delimeter would
make the contents.

1
2
3
4
etc

////////
Consider this statement:

1 Constructors do not have names. A special declarator syntax using
an
optional function-specifier (_dcl.fct.spec_ ) followed by the
construc-
tor's class name followed by a parameter list is used to declare
or
define the constructor. In such a declaration, optional
parentheses
around the constructor class name are ignored. [Example:
class C {
public:
C(); // declares the constructor
};

C::C() { } // defines the constructor
--end example]

Where function specifiers is defined as
1 Function-specifiers can be used only in function declarations.
function-specifier:
inline
virtual
explicit

What is this 'special declaration syntax'? Secondly, am I led to
believe - hence the 'optional function specifier' - that for a
constructor I could do

explicit class C
{
// stuff
};
Jul 22 '05 #1
6 3216
On 3 Aug 2004 10:22:59 -0700, ma740988 <ma******@pegas us.cc.ucf.edu> wrote:
There's no way to use the STL algorithm copy to print an outfile
(essentially an ofstream)? So now:

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::ofstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
// ToFile << InFile.rdbuf();

////////////////////////////
// Alternative 2
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( ToFile, "\n" ) );

// How do I print whats in the 'ToFile'??? Perhaps some other algo?

return 0;
}

Assume the contents of InFile are as follows:
1 2 3 4 5
4 5 6 7 8

The fact that ToFile reflects InFile is a good thing but I'd have
thought the call to ostream_iterato r with the '\n' delimeter would
make the contents.

1
2
3
4
etc


Is this what you are looking for?

#include <algorithm>
#include <iterator>
#include <fstream>

int main()
{
std::ifstream in("input.txt") ;
std::ofstream out("output.txt ");
std::copy(std:: istreambuf_iter ator<char>(in),
std::istreambuf _iterator<char> (),
std::ostreambuf _iterator<char> (out));
}

Using streambuf_itera tor copies a file character by character.

john
Jul 22 '05 #2
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<opsb56zve q212331@androni cus>...
Is this what you are looking for?

#include <algorithm>
#include <iterator>
#include <fstream>

int main()
{
std::ifstream in("input.txt") ;
std::ofstream out("output.txt ");
std::copy(std:: istreambuf_iter ator<char>(in),
std::istreambuf _iterator<char> (),
std::ostreambuf _iterator<char> (out));
}

Using streambuf_itera tor copies a file character by character.

john


What the streambuf_itera tor didn't work for me. What I'm after is
this. I'll copy the contents of the InFile to the ToFile. I could
print the InFile using teh copy algorithm. I'd like to also print the
ToFile.
So now:

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::ofstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();

// Print the contents of the input file.
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( cout, "\n" ) );

// Print the contents of the output (ToFile) file

}
One other question, more from a curiosity perspective. Am I led to
believe that you could add a functional specifier to a constructor.

explicit class C
{
// stuff
};
Jul 22 '05 #3

"ma740988" <ma******@pegas us.cc.ucf.edu> wrote in message
news:a5******** *************** **@posting.goog le.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<opsb56zve q212331@androni cus>...
Is this what you are looking for?

#include <algorithm>
#include <iterator>
#include <fstream>

int main()
{
std::ifstream in("input.txt") ;
std::ofstream out("output.txt ");
std::copy(std:: istreambuf_iter ator<char>(in),
std::istreambuf _iterator<char> (),
std::ostreambuf _iterator<char> (out));
}

Using streambuf_itera tor copies a file character by character.

john
What the streambuf_itera tor didn't work for me. What I'm after is
this. I'll copy the contents of the InFile to the ToFile. I could
print the InFile using teh copy algorithm. I'd like to also print the
ToFile.
So now:

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::ofstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();

// Print the contents of the input file.
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( cout, "\n" ) );

// Print the contents of the output (ToFile) file

}


I see. I don't think you can to that. Why not use an fstream instead of an
ofstream? And probably you should close the file and reopen it.


One other question, more from a curiosity perspective. Am I led to
believe that you could add a functional specifier to a constructor.

explicit class C
{
// stuff
};


I think you mean this

class C
{
public:
explicit C(int n);
};

john
Jul 22 '05 #4
On 5 Aug 2004 05:26:52 -0700, ma******@pegasu s.cc.ucf.edu (ma740988)
wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<opsb56zve q212331@androni cus>...
Is this what you are looking for?

#include <algorithm>
#include <iterator>
#include <fstream>

int main()
{
std::ifstream in("input.txt") ;
std::ofstream out("output.txt ");
std::copy(std:: istreambuf_iter ator<char>(in),
std::istreambuf _iterator<char> (),
std::ostreambuf _iterator<char> (out));
}

Using streambuf_itera tor copies a file character by character.

john
What the streambuf_itera tor didn't work for me. What I'm after is
this. I'll copy the contents of the InFile to the ToFile. I could
print the InFile using teh copy algorithm. I'd like to also print the
ToFile.


By "print", do you mean output to stdout?

You can't print the contents of an output stream - it is a sink, not a
source. You could use an fstream, and then rewind it before outputting
it. e.g.

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::fstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();

// Print the contents of the input file.
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( cout, "\n" ) );

// "Print" the contents of the output (ToFile) file
ToFile.seekg(0, std::ios_base:: beg);
std::cout << ToFile.rdbuf(); //or use std::copy
}


One other question, more from a curiosity perspective. Am I led to
believe that you could add a functional specifier to a constructor.

explicit class C
{
// stuff
};


That is adding a function specifier to a class, which is illegal
(since a class isn't a function). You can do:

class C
{
public:
explicit C();
}

Tom
Jul 22 '05 #5
[...]

You can't print the contents of an output stream - it is a sink, not a
source. You could use an fstream, and then rewind it before outputting
it. e.g.
After reading paragraph 3 or 4 times I'm not sure I'm following "it is
a sink, not a source".

int main()
{
std::ifstream InFile( "exercise15.txt ");
std::fstream ToFile( "NewFile.tx t" );

////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();

// Print the contents of the input file.
std::copy( istream_iterato r<string>(InFil e),
istream_iterato r<string>(),
ostream_iterato r<string>( cout, "\n" ) );

// "Print" the contents of the output (ToFile) file
ToFile.seekg(0, std::ios_base:: beg);
std::cout << ToFile.rdbuf(); //or use std::copy
}

[...]

Got it.. Thanks
Jul 22 '05 #6
On 6 Aug 2004 07:28:16 -0700, ma******@pegasu s.cc.ucf.edu (ma740988)
wrote:
[...]

You can't print the contents of an output stream - it is a sink, not a
source. You could use an fstream, and then rewind it before outputting
it. e.g.


After reading paragraph 3 or 4 times I'm not sure I'm following "it is
a sink, not a source".


A sink is something you can write to, a source is something you can
read from. You can write to, but not read from, an output stream.
Therefore you can't print the contents of an output stream, since in
order to do that you need to read those contents.

Tom
Jul 22 '05 #7

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

Similar topics

5
2656
by: cpp | last post by:
When I create an instance of ofstream, what is the name of the member variable that holds the filename? For example: ofstream ofs("Output.txt"); cout << ofs.WhatIsThePathVariable; If there isn't a public member variable I can use, then is there at least a function that displays the filename?
6
7807
by: Rainer Goerke | last post by:
Hi, Is it possible in a simple way to get the size of the file "myfile.txt" of the ofstream s: ofstream s("myfile.txt", ios::app); Thanks, Rainer
2
2987
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str(); ofstream out0(open_output, ios::out | ios::out);
2
448
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i did below: char filename = {"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};
15
6953
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
2
8972
by: Paul LAURENT | last post by:
Hi everybody, I am using the STL "ofstream" class. I open a file using "ofstream" in update at the end mode ("ate"), I can read and write my file correctly. => What I would like to do is deleting some lines from the file which decreases the size of the file.
11
3431
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
5
3777
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files in total. how do I go about it?, do I set a vector<ofstream*cities; something like
11
2280
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
8704
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
9307
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
9170
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
9009
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
7946
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...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
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
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3155
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.