Practice programming question: file io

  1. Write a function that takes a filename as its parameter and attempts to open the file for reading: if successful it counts and returns the total number of characters in the file, otherwise it returns -1.

  2. Write a function that takes two filenames and an integer, N, as parameters, attempts to open the first for reading and the second for writing, and copies the first N characters of the first file into the second.

  3. Write a function that takes a filename and an array of characters as its two parameters, attempts to open the file for appending: if successful it writes the contents of the array into the file, otherwise it displays an error message.

  4. Write a program that accepts a filename as a command line argument, attempts to open the file for reading, and (if successful) counts and displays the total number of lines in the file.