Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!cernvax!jon From: jon@cernvax.UUCP (jon) Newsgroups: comp.lang.c++ Subject: A beginners problem with C++ Keywords: Files, redirection, cin, cout Message-ID: <973@cernvax.UUCP> Date: 13 Apr 89 15:22:47 GMT Organization: CERN, Geneva, Switzerland Lines: 73 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!" | | | *-----------------------------------------------------------------------*