By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

ofstream, copy algorithm .. more

P: n/a
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
};
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
"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
};
Jul 22 '05 #3

P: n/a

"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
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
[...]

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
Jul 22 '05 #6

P: n/a
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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.