jon@cernvax.UUCP (jon) (04/13/89)
Hi,
  I am new to C++ and to get started I have decided to try converting some
of my existing C program to C++. One of my C programs contains the following,
edited, lines of code.
FILE *infile, *outfile;
  switch (argc) {
    case 1  : infile = stdin;
	      outfile = stdout;
	      break;
    case 2  : if ((infile = fopen(argv[1], "r")) == NULL) {
		fprintf(...);
		exit(1);
	      }
	      outfile = stdout;
	      break;
    case 3  : if ((infile = fopen(argv[1], "r")) == NULL) {
		fprintf(...);
		exit(1);
	      }
	      if ((outfile = fopen(argv[2], "w")) == NULL) {
		fprintf(...);
		exit(1);
	      }
	      break;
    default : fprintf(...);
	      exit(1);
  }
I am having trouble working out how to convert this C++, what I've got
so far is as follows -
filebuf filein, fileout;
  switch (argc) {
    case 1 : ; // Tried everything, nothing works!!!!!!!!
    case 2 : if (filein.open(argv[1], input) == NULL) {
	       cerr << "Error: Cannot open file " << argv[1] << "\n\n";
	       exit(1);
	     }
	     // Likewise
	     break;
    case 3 : if (filein.open(argv[1], input) == NULL) {
	       cerr << "Error: Cannot open file " << argv[1] << "\n\n";
	       exit(1);
	     }
	     if (fileout.open(argv[2], output) == NULL) {
	       cerr << "Error: Cannot open file " << argv[2] << "\n\n";
	       exit(1);
	     }
  }
istream infile(&filein);
ostream outfile(&fileout);
The problem is as follows, how to I define infile to be cin and outfile
to be cout within the confines of a C++ program.
Please if the answer to this is obvious don't flame me ... I melt easily.
The only guide I have to C++ is the Waite Group's book C++ Programming by
John Berry, if anyone can suggest anything else I would be grateful.
*-----------------------------------------------------------------------*
|                                                                       |
|    Jon Caves               {world}!mcvax!cernvax!jon                  |
|    Division DD,            jon@cernvax.cern.ch                        |
|    CERN CH-1211,                                                      |
|    Geneva 23,              "Quote? I haven't got time to think        |
|    Switzerland.               of a quote!"                            |
|                                                                       |
*-----------------------------------------------------------------------*jwolf@hpcupt1.HP.COM (John Wolf) (04/16/89)
Suggestion number 1: don't use stream i/o. It's more complicated than it should be. Use standard C library i/o routines. Just redesign your program in an object oriented fashion (i.e. specify classes). That in itself will be difficult but interesting. John Wolf, HP Cupertino