Lab Assignment -- C++ File IO and Arg Lists


  1. Using C++ write a program called mycat. The program will respond similarly to the "cat" command.
    For instance, a call "mycat filename" will display the contents of filename to the screen.
  2. Add three options to your mycat program. You can use getopt to get these options. The three options are:

For instance, a call like this:

% mycat main.cpp -n -l 20 -f filename

Will result in output similar to the following (the contents of filename will also contain the line numbers).
Notice also that 20 lines are printed and then the user is prompted to press the enter key.

   1 #include <iostream>
   2 using namespace std;
   3
   4 //PART3 need environ
   5 //extern char **environ;
   6
   7 int main(int argc, char *argv[])
   8 {
   9 char *c_ptr;
   10 c_ptr = getenv ("TERM=");
   11
   12 cout << "The variable TERM is " << c_ptr==NULL ? "NOT found" : c_ptr;
   13 //cout << LINES;
   14 /* PART3
   15 for ( ; *environ; ++environ)
   16 cout << *environ<< endl;
   17 */
   18
   19
   
   Press enter to continue
   
   20 /* PART2
   21 int c;
   22 static char optstring[] = "abs:";
   23 opterr=0;
   24
   25 while ((c=getopt(argc, argv, optstring)) != -1)
   26 {
   27 switch(c)
   28 {
   29 case 'a':
   30 cout << "Found option a" << endl;
   31 break;
   32 case 'b':
   33 cout << "Found option b" << endl;
   34 break;
   35 case 's':
   36 cout << "Found option s with an argument of " << optarg << endl;
   37 break;
   38 case '?':
   39 cout << "Found an option that was not in optstring" << endl;
   
   Press enter to continue
   
   40 }
   41 }
   42 if (optind < argc)
   43 cout << "Left off at " << argv[optind];
   44 */
   45 /* PART1
   46 int i;
   47 for (i = 0; i < argc; i++)
   48 {
   49 cout << argv[i] << " ";
   50 }
   51 */
   52 cout << endl;
   53 return 0;
   54 }
   55
   56
   57

Deliverables: