COMP1721 - Higher Computing 1B

Computing 1B - Week 07 Tutorial Questions

  1. Write a C program, numbers.c which when run creates a file "numbers" containing the first 1000 integers, one per line. Your program should print a suitable message if a file can not be opened.

  2. Write a C program, count.c which counts the number of lines and characters in the files whose name is specified on the command line.

    For example:

    % count a b c
    a: 2755 characters 120 lines
    b: 905 characters 46 lines
    c: 460 characters 26 lines
    
    Your program should print a suitable message if a file can not be openned.

  3. The Unix command head -n number file prints the first number lines of a file.

    For example head -n 50 index.html will print the first 50 lines of index.html.

    If the number of lines is not specified, the first 10 lines are printed.

    For example head /tmp/a will print the first 10 lines of /tmp/a.

    If the file has less than the specified number of lines, the entire file will be printed.

    Implement a program head.c which emulates the Unix command head.

    You should print a suitable error message if an exception occurs.


Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW