473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

copy a file to std output

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::stringsvec;

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::ostream_iterator<std::string>( std::cout, "\t" ));
}
return EXIT_SUCCESS;
}
---------------------------------------------------------
int main( int argc, char** argv )
{
std::vector<std::stringsvec;

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;
}
Dec 17 '07 #1
2 2061
On Dec 17, 7:19 am, arnuld <NoS...@NoPain.comwrote:
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::ostreambuf_iterator< char >( std::cout ) ) ;

(An std::ostream_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() ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 17 '07 #2
arnuld wrote:
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::stringsvec;

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::ostream_iterator<std::string>( std::cout, "\t" ));
}
return EXIT_SUCCESS;
}
---------------------------------------------------------
int main( int argc, char** argv )
{
std::vector<std::stringsvec;

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();

--
Jim Langston
ta*******@rocketmail.com
Dec 17 '07 #3

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

Similar topics

2
by: Anna | last post by:
Hi all. I am trying to write a stylesheet that will structure the input HTML file in the following way: For each heading of level n it needs to enclose the heading and all its content until the...
6
by: ma740988 | last post by:
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"...
19
by: Materialised | last post by:
Hi everyone, What I am wanting to do, is to copy, a simple plain text file, to another file, but omitting duplicate items. The way I thought of doing this, involved copying all the items into...
2
by: Bob | last post by:
Cannot copy assembly '<...>' to file <...>.dll'. The process cannot access the file because it is being used by another process. Could not copy temporary files to the output directory. The file...
2
by: Clodoaldo Pinto Neto | last post by:
Hi all, I'm trying to copy a table with a text field column containing a new line char to a file: ksDesenv=# create table page(line text) without oids; CREATE TABLE ksDesenv=# insert into...
7
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped...
4
by: Herman.Schultz | last post by:
Hi, How can I use iostream library in c++ to copy a file from i th byte to an output file? Thank you.
1
by: ajc308 | last post by:
I'm attempting to sort the <file>s within each <directory> in my XML according to their file extension, then write out the resulting sorted data back to XML format. I had it working before, and when...
1
by: RolfK | last post by:
Hello Experts, I have a small problem with copy of CDATA sections. (I'm using XSLT2.0 ) My output target is defined as txt. In my xml source is a CDATA section to be put as it is into the...
3
by: David V | last post by:
I'm having trouble copying DLLs from a folder in my project to the project's output directory. I have copied seven SQL CE DLLs (the SqlCe*.dll set) to a Libs folder in my project. The DLLs VS...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
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...

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.