# 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.. File processing consists of creating, storing, or retrieving the contents of a file from a recognizable medium..

## Objectives and Skills

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

* Pressbooks: Programming Fundamentals
* Wikipedia: File system
* Wikipedia: Directory (computing)
* Wikipedia: Directory structure
* Wikipedia: Text file
* Wikipedia: Binary file

## Multimedia

* YouTube: File Handling and Applications
* YouTube: Introduction To File Processing System

## Examples

* Pseudocode
* BASIC
* C
* C++
* C#
* Java
* Lua
* Node.js
* Perl
* PHP
* PowerShell
* Python3
* Ruby
* Swift
* VB.NET

## Activities

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.
* 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.
* 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.
* 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>
* 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

* 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.
* 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.
* 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.

## Key Terms
### append

    Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.

### close

    Your program requesting the operating system to release a file that was previously opened.

### create

    Opens a file for exclusive creation. If the file already exists, the operation fails.

### device token

    A key value provided by the operating system to associate a device to your program.

### filename

    The name and its extension.

### filespec

    The location of a file along with its filename.

### open

    Your program requesting the operating system to let it have access to an existing file or to open a new file.

### read

    Moving data from a device that has been opened into a memory location defined in your program.

### stream

    A sequence of data elements made available over time.

### stdin

    Standard input stream, typically the keyboard.

### stderr

    Standard output error stream, typically the monitor.

### stdout

    Standard output stream, originally a printer, but now typically the monitor.

### text file

    A file consisting of characters from the ASCII character code set.

### using / with

    A wrapper around a processing block that will automatically close opened resources.

### write

    Moving data from a memory location defined in your program to a device that has been opened.

## Assessments

* Quizlet: Flash Cards
* Quizlet: Quiz