|
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.
- Asks the user for a filename
- Opens and loads the contents of the file
- Prints the contents of the file to the screen, displaying line numbers, or displays a message if the file is empty
- Presents the user with a menu with the following options.
- Add a line to the end of the file
- Delete a line from the file (user specifies line number)
- Insert a line into the file (user specifies line number)
- Repeat the menu (printing the file each time) until the user decides to exit
- 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 |