CMPT 120: Lab 8


Write a function that takes as input a string that is a file name. The function will return a list whose items are the lines in the file. If a line ends with a newline character that character should be removed.
Write a function that basically does the inverse of the first. It takes as input a filename and a list containing only strings. The function should write to the given file the first element of the list to the first line and so forth. A newline character will need to be added to each string to make sure each is on its own line.
Use the above functions to write a program that does the following.
  1. Asks the user for a filename
  2. Opens and loads the contents of the file
  3. Prints the contents of the file to the screen, displaying line numbers, or displays a message if the file is empty
  4. Presents the user with a menu with the following options.
    1. Add a line to the end of the file
    2. Delete a line from the file (user specifies line number)
    3. Insert a line into the file (user specifies line number)
  5. Repeat the menu (printing the file each time) until the user decides to exit
  6. Saves the users changes to the file when exiting
As usual, try and implement the program one part at a time. You can copy the numericInput function from the posted solution to assignment 2 and use it to ensure the user enters a number when a number is asked for.


Chris Schmidt, last updated June 19, 2007