Shuttle Bus

Today we are going to develop a program to look up the table and tell the user when is the departure time of next NCNU shuttle bus.
  1. Derived from the shuttle bus schedule announced by NCNU, we have a plain-text version which can be handled easier in C++. In each line, the three fields represent the weekday, the destination, and the departure time. For example, "0 Puli 12:10" means there will be a shuttle bus bound for Puli at 12:10 on Sunday.
  2. If you want to download a file into your directory on STU, you may type the command "wget http://ipv6.ncnu.org/Course/C_Programming.1022/C/shuttle.txt".
  3. Alternatively, you may simply retrieve a common file which we stored at a public directory "/home/cs2015", as shown in the following example.
  4. To read the contents from a file, you will need the fopen, fscanf, fclose() functions.
    
    int main()
    {
        int d;
        char destination[5];
        char departure_time[6];
        FILE* fp = fopen("/home/cs2015/shuttle.txt", "r");
        while( fscanf(fp, "%d %s %s", &d, destination, departure_time) != EOF )
        {
            cout << d << destination << departure_time << endl;
        }
        fclose(fp);
    }
    
    
  5. Moreover, we want the program to be more flexible, so that in addition to allow a user to query the shuttle bus departure time based on current time, the user can also specify a time, or even a weekday on the command line. For example, "shuttle 14:00 6" tries to look up the next shuttle bus around 14:00 on Saturday.
  6. To get arguments from the command line, as we did in Python, we may inspect the arguments passed from the operating system to our main function. Suppose you have a program:
    
    #include <iostream>
    #include <ctime>
    using std::cout;
    using std::endl;
    
    int main(int argc, char* argv[])
    {
        char now[] = "00:00";
        time_t rawtime;
        time( &rawtime );
        struct tm *timeinfo = localtime( &rawtime );
        strftime(now, 5, "%H:%M", timeinfo);
    
        if (argc == 2)
            strcpy(now, argv[1]);
    
        cout << "Now is " << now << endl;
        return 0;
    }
    
    
    When you run "argv.exe", you get the current time.
    When you run "argv.exe 18:00", we get "Now is 18:00".

The program may run as follows: