copy a file to std output

Discussion in 'C++' started by arnuld, Dec 17, 2007.

  1. arnuld

    arnuld Guest

    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;
    }
    arnuld, Dec 17, 2007
    #1
    1. Advertising

  2. arnuld

    James Kanze Guest

    On Dec 17, 7:19 am, 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 ?


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

    --
    James Kanze (GABI Software) email:
    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
    James Kanze, Dec 17, 2007
    #2
    1. Advertising

  3. arnuld

    Jim Langston Guest

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

    --
    Jim Langston
    Jim Langston, Dec 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page