Computer Programming/Files

From Wikiversity
Jump to: navigation, search

This lesson introduces files and file processing.

Objectives and Skills[edit]

Objectives and skills for this lesson include:

Readings[edit]

  1. Wikipedia: File system
  2. Wikipedia: Directory (computing)
  3. Wikipedia: Directory structure
  4. Wikipedia: Text file
  5. Wikipedia: Binary file

Multimedia[edit]

  1. YouTube: File Handling and Applications

Examples[edit]

Activities[edit]

  1. Create a text file of names and grade scores to use for testing based on the following format:
        Larry Fine: 80
        Curly Howard: 70
        Moe Howard: 90
    Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and display the high, low, and average score. Use separate subroutines/functions/methods to implement each type of processing. Avoid global variables by passing parameters and returning results.
  2. Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and add each score to an array. Display the array contents and then calculate and display the high, low, and average score. Use separate subroutines/functions/methods to implement each type of processing. Avoid global variables by passing parameters and returning results.
  3. Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and add each score to an array. Display the array contents and then calculate and display the high, low, and average score. Include error handling in case the file is formatted incorrectly. Use separate subroutines/functions/methods to implement each type of processing. Avoid global variables by passing parameters and returning results.
  4. Create a program that asks the user for the name of a text/HTML file that contains HTML tags, such as:
        <p><strong>This is a bold paragraph.</strong></p>
    Verify that the file exists and then use string methods to search for and remove all HTML tags from the text, saving each removed tag in an array. Display the untagged text and then display the array of removed tags. For example:
        This is a bold paragraph.

        <p>
        <strong>
        </strong>
        </p>
    Use separate subroutines/functions/methods to implement each type of processing. Avoid global variables by passing parameters and returning results.
  5. Create a text file of names and addresses to use for testing based on the following format:
        Firstname Lastname
        123 Any Street
        City, State/Province/Region PostalCode
    Include a blank line between addresses, and include at least three addresses in the file. Create a program that verifies that the file exists, and then processes the file and displays each address as a single line of comma-separated values in the form:
         Lastname, Firstname, Address, City, State/Province/Region, PostalCode
    Use separate subroutines/functions/methods to implement each type of processing. Avoid global variables by passing parameters and returning results.

Assessments[edit]

See Also[edit]

References[edit]