copy a file to std output

A

arnuld

I have 2 programs.

1st PROGRAM: copies an input file to standard output but it
does so at the expense of deleting all white-space
(space, tabs, newlinies etc).

2nd PROGRAM: copies the input as it is to the standard output.



WHAT I WANT:

I want to merge the 2 programs, I want to do what 2nd program does
by replacing "while loop" with "std::copy" as in 1st program.

any idea ?




int main( int argc, char** argv )
{
std::vector<std::string> svec;

if( argc < 2 )
{
std::cerr << "No input file\n" << std::endl;
return EXIT_FAILURE;
}
else
{
std::ifstream infile( argv[1] );

std::copy( std::istream_iterator<std::string>( infile ),
std::istream_iterator<std::string>(),
std::eek:stream_iterator<std::string>( std::cout, "\t" ));
}


return EXIT_SUCCESS;
}


---------------------------------------------------------


int main( int argc, char** argv )
{
std::vector<std::string> svec;

if( argc < 2 )
{
std::cerr << "No input file\n" << std::endl;
return EXIT_FAILURE;
}
else
{
std::ifstream infile( argv[1] );
std::string aline;
while( getline( infile, aline ))
{
std::cout << aline << std::endl;
}

}


return EXIT_SUCCESS;
}
 
J

James Kanze

I have 2 programs.
1st PROGRAM: copies an input file to standard output but it
does so at the expense of deleting all white-space
(space, tabs, newlinies etc).
2nd PROGRAM: copies the input as it is to the standard output.
WHAT I WANT:
I want to merge the 2 programs, I want to do what 2nd program does
by replacing "while loop" with "std::copy" as in 1st program.
any idea ?

Formatted input removes the white space, so use unformatted. Or
in the case of stream iterators, an istreambuf_iterator<char>,
rather than an istream_iterator:

std::copy( std::istreambuf_iterator< char >( infile ),
std::istreambuf_iterator< char >(),
std::eek:streambuf_iterator< char >( std::cout ) ) ;

(An std::eek:stream_iterator would also work for output. But I
prefer the consistency of doing both input and output at the
same level.)

Of course, the "standard" way of copying one stream into another
is:

std::cout << infile.rdbuf() ;
 
J

Jim Langston

arnuld said:
I have 2 programs.

1st PROGRAM: copies an input file to standard output but it
does so at the expense of deleting all white-space
(space, tabs, newlinies etc).

2nd PROGRAM: copies the input as it is to the standard output.



WHAT I WANT:

I want to merge the 2 programs, I want to do what 2nd program does
by replacing "while loop" with "std::copy" as in 1st program.

any idea ?




int main( int argc, char** argv )
{
std::vector<std::string> svec;

if( argc < 2 )
{
std::cerr << "No input file\n" << std::endl;
return EXIT_FAILURE;
}
else
{
std::ifstream infile( argv[1] );

std::copy( std::istream_iterator<std::string>( infile ),
std::istream_iterator<std::string>(),
std::eek:stream_iterator<std::string>( std::cout, "\t" ));
}


return EXIT_SUCCESS;
}


---------------------------------------------------------


int main( int argc, char** argv )
{
std::vector<std::string> svec;

if( argc < 2 )
{
std::cerr << "No input file\n" << std::endl;
return EXIT_FAILURE;
}
else
{
std::ifstream infile( argv[1] );
std::string aline;
while( getline( infile, aline ))
{
std::cout << aline << std::endl;
}

}


return EXIT_SUCCESS;
}

Easy way:

std::ifstream infile( argv[1] );
std::cout << infile.rdbuf();
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top