Lab 10: File Access

Lab 10:  File Access
This lab accompanies Chapter 10 of Starting Out with Programming Logic & Design.
Lab 10.1 – File Access and Pseudocode
Critical Review
When a program needs to save data for later use, it writes the data in a file.  The data can be read from the file at a later time.
Three things must happen in order to work with a file.  1) Open a file.  2) Process the file.  3)  Close the file.
An internal file must be created for an output file or input file, such as:
     Declare OutputFile myFile  //to write out
     Declare InputFile myFile   //to read in
A data file must also be created to store the output, such as:
   
     Open myFile “thedata.txt”
New keywords and syntax include the following:
    
            Open [InternalName] [FileName]
     Write [InternalName] [String or Data]
     Read [InternalName] [Data]
     Close [InternalName]
     AppendMode  //used with Open when need to append
Loops are used to process the data in a file.  For example:
     For counter = 1 to 5
        Display “Enter a number:”
        Input number
        Write myFile number
     End For
When reading information from a file and it is unknown how many items there are, use the eof function.  For example:
     While NOT eof(myFile)
        Read myFile number
        Display number
     End While
This lab examines how to work with a file by writing pseudocode.  Read the following programming problem prior to completing the lab.  The following program from Lab 9.1 will be used, with some modifications. 
The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive.  The program should take in the number of pints donated during the drive, based on a seven hour drive period.  The average pints donated during that period should be calculated and written to a file.  Write a loop around the program to run multiple times.  The data should be appended to the file to keep track of multiple days.  If the user wants to print data from the file, read it in and then display it.  Store the pints per hour and the average pints donated in a file called blood.txt.
Step 1:  Note that the getPints, getTotal, and getAverage functions do not change.  Also note that the references to displayInfo, getHigh, and getLow functions are removed to meet the new requirements.  In the pseudocode below, add the following:
In the Main Module
A variable named option of the data type Integer
Input option
Write an if statement that will determine which option to run
Call a module called writeToFile that passes pints and averagePints
Call a module called readFromFile that passes pints and averagePints
In the writeToFile Module
Declare an output file called outFile in AppendMode, with the name bloodFile.  (Reference: Appending Data to an Existing File, Page 370).
Open the internal file (bloodFile) and a text file named blood.txt.  (Reference: Creating a File and Writing Data to it, Page 362.)
Write the string “Pints Each Hour” to the file. (Reference: Writing Data to a File, Page 363).
In the while loop, write each element of the pints array to the bloodFile.  (Reference: Using Loops to Process Files, Page 371).
Write the string “Average Pints” to the file.
Write the value of averagePints to the file.
Close the bloodFile. (Reference: Closing an Output File, Page 363).
In the readFromFile Module
Declare an input file called inFile , with the name bloodFile.  (Reference: Reading Data from a File, Page 366).
Open the internal file (bloodFile) and a text file named blood.txt.
Read the string “Pints Each Hour” in from your file and store into a variable str1.  This should be done such as Read bloodFile str1.  The string will be stored in the variable str1.
Display str1 to the screen.
Read pints in from the bloodFile and store in the pints array.
Display pints to the screen.
Read the string “Average Pints” in from your file and store into a variable str2.
Display str2 to the screen.
Read averagePints in from the bloodFile.
Display averagePints to the screen
Close the file.  (Reference: Closing an Input File, Page 367).
Module main()
            //Declare local variables
            Declare String again = “no”
            Declare Real pints[7]
            Declare Real totalPints
            Declare Real averagePints
            a.  _________________________________________
                      
            While again == “no”
                        //module calls below
                        Display “Enter 1 to enter in new data and store to file”
                        Display “Enter 2 to display data from the file”
                        Input b.  ______________________________
                        c.  If _____________ == _________________ Then
                                    Call getPints(pints)
                                    Call getTotal(pints, totalPints)
                                    Call getAverage(totalPints, averagePints)
                                    d.  Call _____________(__________,________)
                        Else
                                    e.  Call _____________(__________,________)
                        End If
                        Display “Do you want to run again: yes or no”
                        Input again
            End While
End Module
Module getPints(Real pints[])
Declare Integer counter = 0
                        For counter  = 0 to 6
                                    Display “Enter pints collected:”
                                    Input pints[counter]
                        End For
End Module
Function getTotal(Real pints[], Real totalPints)
Declare Integer counter = 0
Set totalPints = 0
                        For counter = 0 to 6
                                    Set totalPints = totalPints + pints[counter]                           End For
Return totalPints
          
            Function getAverage(Real totalPints, Real averagePints)
averagePints = totalPints / 7
Return averagePints
          
            Module writeToFile(Real pints[], Real averagePints)
                        f.  Declare _________ ____________ ____________
                        g.  Open _________________ ____________________
                        h.  Write _______________ “__________________”
                            Declare Integer counter = 0
                        i.  While counter < 7
                                      Write __________ _________[_________]
                                      Set counter = counter + 1
                            End While
j.  Write ____________ “_____________”
k.  Write ____________ ________________
l.  Close __________________
            End Module
            Module readFromFile(Real pints[], Real averagePints)
m.  Declare ___________  ___________
                        n.  Open __________ “__________”
                        o.  Read _____________ ____________
                        p.  Display ____________
                        q.  Read ______________ _____________
                        r.  Display _______________
                        s.  Read ___________ ____________
                        t.  Display ______________
                        u.  Read _____________ _______________
                        v.  Display _____________
                        w.  Close _______________
            End Module

Lab 10.2 – File Access and Flowcharts
 

Critical Review
Outputting to a File using Raptor
The Output symbol is used to output data to a text file.  When an Output symbol is reached during Raptor program execution, the system determines whether or not output has been redirected.  If output has been redirected, meaning an output file has been specified, the output is written to the specified file.  If output has not been redirected, it goes to the Master Console.
One version of redirecting output to a file is by creating a call symbol and adding the following:
        Redirect_Output(“file.txt")
Note: If the file specified already exists, it will be overwritten with no warning!  All of the file's previous contents will be lost!
The second version of Redirect_Output redirects output with a simple yes or true argument:
            Redirect_Output(True)
This delays the selection of the output file to run time.  When the Call symbol containing Redirect_Output is executed, a file selection dialog box will open, and the user can specify which file is to be used for output.
After a successful call to Redirect_Output, the program writes its output to the specified file.  To reset Raptor so that subsequent Output symbols write their output to the Master Console, another call to Redirect_Output is used, this time with a False (No) argument:
Redirect_Output(False)
After this call is executed, the output file is closed, and subsequent outputs will again go to the Master Console.
There is no Append option in Raptor.
Input to a File using Raptor
This is done the same way, except Redirect_Input( ) is called.
To pull something in from a file, the input symbols are used.
 


This lab requires you to create a flowchart for the blood drive program in Lab 10.1.  Use an application such as Raptor or Visio.
Step 1:  Start Raptor and open your Lab 9-3.  Save this file as Lab 10-3.  The .rap file extension will be added automatically.
Step 2:  Remove the reference no longer need.  This is the highPints and lowPints variables, and the getHigh, getLow, and displayInfo modules.  With the modules, first delete the function calls, and the right click on the tabs and select Delete Subchart.
Step 3:  In main after the module call to getAverage, add a call to writeToFile.
Step 4:  Go to that module and add a call symbol.  Add the following:  Redirect_Output("blood1.txt").
Step 5:  Add an output symbol that prints the string “Pints Each Hour”.
Step 6:  Add an assignment symbol that sets counter to 1.
Step 7:  Add a loop symbol that has the condition of counter 7.
Step 8:  If it is False, add an output symbol that prints pints[counter] to the file.  This should look as follows:
Step 9:  Add an assignment statement that increments counter by 1.
Step 10:  If it is True, add an output symbol that prints the string “Average Pints” to the file.
Step 11:  Add an output symbol that prints averagePints to the file.
Step 12:  Add a call symbol that closes the file.  This should look as follows:
Step 13:  In main after the call to writeToFile, add a call to readFromFile.
Step 14:  In the readFromFile module, add a call symbol to Redirect_Input, such as Redirect_Input("blood1.txt").
Step 15:  Add an Input symbol that gets str1.  This should look as follows:
Step 16:  Add an assignment statement that sets counter to 1.
Step 17:  Add a loop statement.  If the loop is False, get the next value from the file and store it in pints[counter].  This should look as follows:
Step 18:  Increment counter by 1.
Step 19:  If the loop is True, get str2 with an input symbol.
Step 20:  Add an input symbol that gets averagePints.
Step 21:  Add a call symbol that sets Redirect_Input to True.
Step 22:  In the Main module, add an input symbol under the loop symbol.  This should ask the user to enter 1 if they want to take in data and add to the file or 2 if they want to print information from the file.  Store this in a variable called option.
Step 23:  Add a decision symbol that asks if option is equal to 1.  If it is, call the getPints, getTotal, getAverage, and writeToFile module.  If it is not, call the readFromFile module.
Step 24:  Run your program once and be sure to select option 1 on the first time.  This will create a file called blood1.txt in your directory where your Raptor flowchart is located.  An example file might contain the following:
Pints Each Hour
45
34
23
54
34
23
34
Average Pints
35.2857
Step 25:  Go to your file called blood1.txt and examine the contents.  Paste the contents below.
            PASTE CONTENTS HERE
Step 26:  Run your program again, but select option 2.  You can test to see if it is reading values properly into your program by examining the contents of the variables that are listed on the left.  The following is an example:

Step 27:  Paste your finished flowchart in the space below.
            PASTE FLOWCHART HERE

Lab 10.3 – File Access and Python Code
The goal of this lab is to convert the blood drive program from Lab 10.1 to Python code.
Step 1:  Start the IDLE Environment for Python.  Prior to entering code, save your file by clicking on File and then Save.  Select your location and save this file as Lab10-3.py.  Be sure to include the .py extension.
Step 2:  Document the first few lines of your program to include your name, the date, and a brief description of what the program does.
Step 3:  Start your program with the following code:
#Lab 10-3 Blood Drive
#the main function
def main():
  endProgram = 'no'
  print
  while endProgram == 'no':
    option = 0
    print
    print 'Enter 1 to enter in new data and store to file'
    print 'Enter 2 to display data from the file'
    option = input('Enter now -')
    print
  
    # declare variables
    pints = [0] * 7
    totalPints = 0
    averagePints = 0
    if option == 1:
      # function calls
      pints = getPints(pints)
      totalPints = getTotal(pints, totalPints)
      averagePints = getAverage(totalPints, averagePints)
    
    else:
    
 
    endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
    while not (endProgram == 'yes' or endProgram == 'no'):
      print 'Please enter a yes or no'
      endProgram = raw_input('Do you want to end program? (Enter no or yes): ')
#the getPints function
def getPints(pints):
  counter = 0
  while counter < 7:
      pints[counter] = input('Enter pints collected: ')
      counter = counter + 1
  return pints
#the getTotal function
def getTotal(pints, totalPints):
  counter = 0
  while counter < 7:
    totalPints = totalPints + pints[counter]
    counter = counter + 1
  return totalPints
#the getAverage function
def getAverage(totalPints, averagePints):
  averagePints = float(totalPints) / 7
  return averagePints
#the writeToFile function
def writeToFile(averagePints, pints):
#the readFromFile function
            def readFromFile(averagePints, pints):
# calls main
main()
Step 4:  Under option 1 in main, add a function call to writeToFile and pass it averagePints and pints.  This should be done after the other calls.  This should look as follows:
      writeToFile(averagePints, pints)
Step 5:  Under option 2 in main, add a function call to readFromFile and pass it averagePints and pints.  This should be done after the other calls.  This should look as follows:
      readFromFile(averagePints, pints)
Step 6:  Under the documentation and the function header for the writeToFile function, create an outFile and call the open function.  Pass this function the name of the text file and open it in append mode.  This should look as follows:
  outFile = open('blood.txt', 'a')
Step 7:  The next step is to write the string ‘Pints Each Hour’ to the file.  This is done as follows:
  print outFile, 'Pints Each Hour'
Step 8:  Initial counter to 0 and add a while loop with the condition of counter < 7.  Inside the while loop, write the value of the array pints to the file.  This should look as follows:
    outFile.write(str(pints[counter]) + '\n')
    counter = counter + 1
Step 9:  Outside the while loop, write the string ‘Average Pints’ to the file.
Step 10:  Next, write the averagePints variable to the file.  This should look as follows:
  outFile.write(str(averagePints) + '\n\n')
Step 11:  The last item in this function is to close the outFile.  This is done as follows:
  outFile.close()
Step 12:  Under the documentation and the function header for the readFromFile function, create an inFile and call the open function.  Pass this function the name of the text file and open it in read mode.  This should look as follows:
  inFile = open('blood.txt', 'r')
Step 13:  Next, read in the string ‘Pints Each Hour’ and print this to the screen.  This is done as follows:
  str1 = inFile.read()
  print str1
Step 14:  Read in the pints array as an entire list and print this to the screen.  This is done as follows:
  pints = inFile.read()
  print pints
  print  #adds a blank line
Step 15:  Read in the string ‘Average Pints’ and print this to the screen.
Step 16:  Read in averagePints and print this to the screen.
Step 17:  Close the inFile.
Step 18:  Run your program and for the first execution, select option 1.  Run the program more than once and enter at least 2 sets of data.  The append mode should keep track of everything.  The contents of your file will be stored in the same directory that your Python code is in.  Paste the contents of the file below:
PASTE CONTENTS HERE
Step 19:  Run your program again and select option 2 on the first iteration.  This should display to the screen information that is stored in your file.
Step 20:  Execute your program so that it works and paste the final code below
          
PASTE CODE HERE
Powered by