Michael DOUBEZ
11/5/2008 1:49:00 PM
arnuld a écrit :
> This one works to seem fine. Can I make this program better ?
>
> 1) the use of get(ch) function was inspired from Stroustrup 21.5.1, page
> number 638.
You can also use std::copy with istream_ierator/ostream_iterator.
There is also the usage of rdbuf().
> 2) I see, when you create an object of std::ifstream while passing a
> pointer to it, it automatically opens the file.
Yes and since it is in a function you could also omit closing it since
it is closed on destruction.
> 3) If I open a file using std::ostream, then I am confused whether it
> will open the file for writing or appending ?.
Do you mean std::ofstream ?
Is so, the default is opened in write mode at the beginning of the file;
there is a second parameter you can specify to open in append mode.
There are other flags.
If you mean std::ostream, you cannot directly open a file with it but
you can use a filebuf:
std::filebuf fbuf;
fbuf.open(filename,ios::out);
std::ostream os(&fbuf);
//now os will write into the file
>
>
>
> /* Section 7.10: Exercise 4
> *
> * Write a program that reads arbitrary number of files (whose names are
> * given as command-line arguments) and writes them to one after another
> * on std::cout.
> *
> * My view: It feels like UNIX cat
> *
> * VERSION: 1.0
> *
> */
>
> #include <iostream>
> #include <cstdlib>
> #include <fstream>
> #include <algorithm>
> #include <iterator>
>
>
> int print_file( char* );
I suppose you don't intend to modify the filename; use const-correctness
specifiers:
int print_file( const char* );
>
>
> int main(int argc, char* argv[] )
> {
> if( 1 == argc )
> {
> std::cerr << "No Input File\n";
> exit( EXIT_FAILURE );
> }
>
>
> int file_count = (argc - 1);
> int idx = 1;
> while( file_count-- )
Decrementing the number of file and incrementing an index looks clumsy
and prone to error. I'd rather use a for loop:
for(int idx=1;idx<argc;++idx)
> {
For clarity, use a constant name:
char const * const filename=argv[idx];
And use this name hereafter instead of argv[idx].
> if( print_file( argv[idx] ) )
> {
> std::cerr << "error reading file: "
> << argv[idx]
> << std::endl;
> }
>
> ++idx;
> }
>
>
> return 0;
To be consistent with your EXIT_FAILURE:
return EXIT_SUCCESS;
> }
>
>
>
>
> int print_file( char* pc )
int print_file( const char* pc )
> {
> const int read_success = 0;
> const int read_failure = 1;
>
> std::ifstream ifile(pc);
>
> if( (!ifile) )
> {
> ifile.close();
> return read_failure;
> }
>
> char ch;
> while( ifile.get(ch) )
> {
> std::cout << ch;
> }
There is a trick to output a sink into another:
if(!std::cout<<ifile.rdbuf())
{
return read_failure;
}
>
> ifile.close();
>
> return read_success;
> }