Unit 10 Assignment1
Short Answer Review Question
Described the three steps that must be taken when a file is used by a program.
Open the file – it creates a connection between the file and the program. Opening and output file usually creates the file on the disk and allow the program to write data to it. Opening an input file allows the program to read data from the file.
Process the file – In this step data is either written to the file (if it is an output file) or read from the file (if it is an input file).
Close the file – When the program is finished using the file, the file must be closed. Closing a file disconnects the file from the program.
Why should a program close a file when it’s finished using it?
By closing a file, any unsaved data that is held in the buffer is saved to its file. Also, when a program closes files are no longer being used; the files will not take up more of the OS’s resources than necessary.
If an existing file is opened in append mode, what happens to the file existing contents?
Appending data to a file means writing new data to the end of the data that already exist in the file. Most languages allow you to open an output file in append mode, which means: if the file already exist, it will not be erased. If the file does not exist, it will be created. And when data is written to the file, it will be written at the end of the file’s current contents.
In most languages, if a file does not exist and a program attempts to open it in append mode, what happen?
If the file does not exist, it will be created.
Algorithm Workbench
Design a program that opens an output file with the external name my_name.dat, write your name to the file, and then close the file.
Declare OutputFile myFile Open myFile "my_name.dat" Write myFile "Power" Close myFile
Design a program that opens the my_name.dat file that was created by the algorithm in question 1, reads your name from the file, display the name on the screen, and then closes the file.