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.txt" );
////////////////////////////
// Alternative 1
// ToFile << InFile.rdbuf();
////////////////////////////
// Alternative 2
std::copy( istream_iterator<string>(InFile),
istream_iterator<string>(),
ostream_iterator<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_iterator 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
}; 6 3193
On 3 Aug 2004 10:22:59 -0700, ma740988 <ma******@pegasus.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.txt" );
//////////////////////////// // Alternative 1 // ToFile << InFile.rdbuf();
//////////////////////////// // Alternative 2 std::copy( istream_iterator<string>(InFile), istream_iterator<string>(), ostream_iterator<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_iterator 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_iterator<char>(in),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(out));
}
Using streambuf_iterator copies a file character by character.
john
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsb56zveq212331@andronicus>... 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_iterator<char>(in), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); }
Using streambuf_iterator copies a file character by character.
john
What the streambuf_iterator 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.txt" );
////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();
// Print the contents of the input file.
std::copy( istream_iterator<string>(InFile),
istream_iterator<string>(),
ostream_iterator<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
};
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m... "John Harrison" <jo*************@hotmail.com> wrote in message
news:<opsb56zveq212331@andronicus>... 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_iterator<char>(in), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); }
Using streambuf_iterator copies a file character by character.
john What the streambuf_iterator 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.txt" );
//////////////////////////// // Alternative 1 ToFile << InFile.rdbuf();
// Print the contents of the input file. std::copy( istream_iterator<string>(InFile), istream_iterator<string>(), ostream_iterator<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
On 5 Aug 2004 05:26:52 -0700, ma******@pegasus.cc.ucf.edu (ma740988)
wrote: "John Harrison" <jo*************@hotmail.com> wrote in message news:<opsb56zveq212331@andronicus>...
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_iterator<char>(in), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); }
Using streambuf_iterator copies a file character by character.
john What the streambuf_iterator 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.txt" );
////////////////////////////
// Alternative 1
ToFile << InFile.rdbuf();
// Print the contents of the input file.
std::copy( istream_iterator<string>(InFile),
istream_iterator<string>(),
ostream_iterator<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
[...] 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.txt" );
//////////////////////////// // Alternative 1 ToFile << InFile.rdbuf();
// Print the contents of the input file. std::copy( istream_iterator<string>(InFile), istream_iterator<string>(), ostream_iterator<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
On 6 Aug 2004 07:28:16 -0700, ma******@pegasus.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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();...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Dijkstra |
last post by:
Hi folks!
First, this is the code I'm using to expose the problem:
------------------------------------------------------------------
#include <functional>
#include <string>
#include...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| | |