Programming Fundamentals/Files

This lesson introduces files and file processing. A File is an object on a computer that stores data, information, settings, or commands used with a computer program.[1]. File processing consists of creating, storing, or retrieving the contents of a file from a recognizable medium.[2].

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. Pressbooks: 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
  2. YouTube: Introduction To File Processing System

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. Note: Each of the following activities uses code only to read the file. It is not necessary to use code to create the file.

  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:
        <h1>Strings and Files</h1>
        <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:
        Strings and Files
        This is a bold paragraph.

        <h1>
        </h1>
        <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.[3]
  • 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.[4]
  • 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.[5]

Key Terms

edit
append
Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.[6]
close
Your program requesting the operating system to release a file that was previously opened.[7]
create
Opens a file for exclusive creation. If the file already exists, the operation fails.[8]
device token
A key value provided by the operating system to associate a device to your program.[9]
filename
The name and its extension.[10]
filespec
The location of a file along with its filename.[11]
open
Your program requesting the operating system to let it have access to an existing file or to open a new file.[12]
read
Moving data from a device that has been opened into a memory location defined in your program.[13]
stream
A sequence of data elements made available over time.[14]
stdin
Standard input stream, typically the keyboard. [15]
stderr
Standard output error stream, typically the monitor.[16]
stdout
Standard output stream, originally a printer, but now typically the monitor.[17]
text file
A file consisting of characters from the ASCII character code set.[18]
using / with
A wrapper around a processing block that will automatically close opened resources.[19]
write
Moving data from a memory location defined in your program to a device that has been opened.[20]

Assessments

edit

See Also

edit

References

edit
  1. "File". Computer Hope. 2018-08-03. https://www.computerhope.com/jargon/f/file.htm. 
  2. "Introduction to File Processing". Yevol. 2007. http://www.yevol.com/en/vccli/Lesson10.htm. 
  3. "Standard streams". Wikipedia. 2019-03-05. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=886304371. 
  4. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  5. Braunschweig, Dave; Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/loading-an-array-from-a-text-file/. 
  6. https://www.w3schools.com/python/python_file_handling.asp
  7. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  8. https://www.w3schools.com/python/python_file_handling.asp
  9. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  10. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  11. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  12. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  13. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  14. "Stream (computing)". Wikipedia. 2019-10-01. https://en.wikipedia.org/w/index.php?title=Stream_(computing)&oldid=918985904. 
  15. "Standard streams". Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  16. "Standard streams". Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  17. "Standard streams". Wikipedia. 2019-10-11. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=920720336. 
  18. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  19. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/. 
  20. Busbee, Kenneth Leroy. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/.