ofstream, copy algorithm .. more

M

ma740988

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::eek:fstream 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
};
 
J

John Harrison

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::eek:fstream 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::eek:fstream out("output.txt");
std::copy(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>(),
std::eek:streambuf_iterator<char>(out));
}

Using streambuf_iterator copies a file character by character.

john
 
M

ma740988

John Harrison said:
Is this what you are looking for?

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

int main()
{
std::ifstream in("input.txt");
std::eek:fstream out("output.txt");
std::copy(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>(),
std::eek:streambuf_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::eek: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

}


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
};
 
J

John Harrison

ma740988 said:
"John Harrison" <[email protected]> wrote in message

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::eek: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

}

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
 
T

tom_usenet

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
 
M

ma740988

[...]
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
 
T

tom_usenet

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top