Assignment 1, Due at 11:59pm, Sunday Sep 07


What to do

You are to write a C++ program that does the following.
  1. It keeps reading user's inputs, line by line. Each input line the user types is supposed to be in one of the following three forms:
    numcols filename
    numrows filename
    • filename is the name of a text file. The file contains a set of lines; each line is a list of words separated by spaces or tabs. Each word is a series of (ASCII) characters with no tab nor space in between them. For example, the content of the file test.txt might look like this
       this is    a file
       that has seven columns and four rows. 
       Note that rows might have 
       different numbers of columns.
      In this file, the first row has 4 words, second has 7, third has 5, and fourth has 4.
    • numcols stands for "number of columns" of filename, which is the maximum number of words that a row has.
    • numrows stands for "number of rows", which is the number of lines in the file.
    • exit tells your program to quit
  2. If the user types exit, then your program quits.
  3. The other two commands numrows and numcols reports the number of columns and the number of rows the file has.
  4. For example, if the file test.txt has the content as shown above, then the output of the program looks as follows.
    > numrows test.txt
    > numcols test.txt
    > numcols anothertest.txt
    > exit
    Here, anothertest.txt is some other file that has 6 columns. Your program has to keep running until the user types exit.

My implementation

I have written a program named txtparser following the above specification and compiled it under timberlake. You can download and run it (in timberlake) to see how it is supposed to work. You can download it by clicking on the link above (and upload the file to timeberlake), or you can log on to your CSE account on timberlake, then type
Remember to make it an executable file before trying to run it:
chmod 700 txtparser
You can also get two test files using the same method:

How to submit

Put all files in a directory named A1, then follow the steps similar to assignment 0. You do need to construct a Makefile for it. If your cpp source file is named txtparser.cpp, then your Makefile might have the following content
    g++ txtparser.cpp -o txtparser

    rm -f txtparser
To submit, perform similar steps as in assignment 0.
cd ..
tar -cvf A1.tar A1/*
submit_cse250 A1.tar
Note that the last line only works if you logged in to your CSE account and the tar file is there. All previous things can be done at home, as long as you remember to upload the final tar file to your CSE account and run the submit script from there.


Supporting materials

Read Chapter 1 of the textbook if you needed to. Read the Getting started in C++ lecture note. The "reverse characters, reverse words" example already showed you how to get input lines from the user. The following snippet of code shows you how to read a file, one line at a time. Overall, you have all of the tools available to write this program.
#include <iostream>                                                       
#include <fstream>                                                        
using namespace std;                                                            
int main() 
    string fname, line;                                                         
    ifstream ifs; // input file stream                                          
    int i;                                                                      
    cout << "---- Enter a file name : ";                                        
    while (getline(cin, fname)) { // Ctrl-Z/D to quit!                          
        // tries to open the file whose name is in string fname                ;                                                
        if ( {                                                       
            cerr << "ERROR: Failed to open file " << fname << endl;   
        } else {                                                                
            i = 0;                                                              
            while (getline(ifs, line))                                          
                cout << "Line " << i++ << " : " << line << endl;                
            ifs.close(); // always remember to close it                         
        cout << "---- Enter another file name : ";                              
    return 0;