Programming Fundamentals/Files

From Wikiversity
Jump to navigation Jump to search

This lesson introduces files and file processing.

Objectives and Skills[edit]

Objectives and skills for this lesson include:

  • Understand file input and output
  • Validate file open, edit, and close operations
  • Use error handling with file operations tasks

Readings[edit]

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

Multimedia[edit]

  1. YouTube: File Handling and Applications

Examples[edit]

Activities[edit]

Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use separate functions for input, each type of processing, and output. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.

  1. Using a text editor or IDE, copy the following list of names and grade scores and save it as a text file named scores.txt:
        Name,Score
        Joe Besser,70
        Curly Joe DeRita,0
        Larry Fine,80
        Curly Howard,65
        Moe Howard,100
        Shemp Howard,85
    Create a program that displays high, low, and average scores based on input from scores.txt. 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. Format the average to two decimal places. Note that the program must work for any given number of scores in the file. Do not assume there will always be six scores.
  2. Create a program that displays high, low, and average scores based on input from scores.txt. 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. Format the average to two decimal places. Include error handling in case the file is formatted incorrectly. Note that the program must work for any given number of scores in the file. Do not assume there will always be six scores.
  3. 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>
  4. Using a text editor or IDE, 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

Lesson Summary[edit]

  • In computer programming, standard streams are pre-connected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin – keyboard), standard output (stdout – originally a printer) and standard error (stderr – monitor). Streams may be redirected to other devices and/or files. In current environments, stdout is usually redirected to the monitor.[1]
  • It is considered good programming practice to determine if the file was opened properly. The reason the operating system usually can’t open a file is because the filespec is wrong (misspelled or not typed case consistent in some operating systems) or the file is not stored in the location specified. Accessing files stored on a network or the Internet may fail due to a network error.[2]
  • Loading an array from a text file requires several steps, including: opening the file, reading the records, parsing (splitting) the records into fields, adding the fields to an array, and closing the file. The file may be read all at once and then parsed, or processed line by line. The array must either be at least as large as the number of records in the file, or must be generated dynamically.[3]

Key Terms[edit]

close
Your program requesting the operating system to release a file that was previously opened[4]
device token
A key value provided by the operating system to associate a device to your program.[5]
filename
The name and its extension.[6]
filespec
The location of a file along with its filename.[7]
open
Your program requesting the operating system to let it have access to an existing file or to open a new file.[8]
read
Moving data from a device that has been opened into a memory location defined in your program.[9]
stream
A sequence of data elements made available over time.[10]
stdin
Standard input stream, typically the keyboard. [11]
stderr
Standard output error stream, typically the monitor.[12]
stdout
Standard output stream, originally a printer, but now typically the monitor.[13]
text file
A file consisting of characters from the ASCII character code set.[14]
using / with
A wrapper around a processing block that will automatically close opened resources.[15]
write
Moving data from a memory location defined in your program to a device that has been opened.[16]

Assessments[edit]

See Also[edit]

References[edit]

  1. "Standard streams" (in en). Wikipedia. 2019-03-05. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=886304371. 
  2. Busbee, Kenneth Leroy. Programming Fundamentals.
  3. Braunschweig, Dave; Busbee, Kenneth Leroy. Programming Fundamentals.
  4. Busbee, Kenneth Leroy. Programming Fundamentals.
  5. Busbee, Kenneth Leroy. Programming Fundamentals.
  6. Busbee, Kenneth Leroy. Programming Fundamentals.
  7. Busbee, Kenneth Leroy. Programming Fundamentals.
  8. Busbee, Kenneth Leroy. Programming Fundamentals.
  9. Busbee, Kenneth Leroy. Programming Fundamentals.
  10. "Stream (computing)" (in en). Wikipedia. 2019-10-01. https://en.wikipedia.org/w/index.php?title=Stream_(computing)&oldid=918985904. 
  11. "Standard streams" (in en). Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  12. "Standard streams" (in en). Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  13. "Standard streams" (in en). Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  14. Busbee, Kenneth Leroy. Programming Fundamentals.
  15. Busbee, Kenneth Leroy. Programming Fundamentals.
  16. Busbee, Kenneth Leroy. Programming Fundamentals.