100 likes | 204 Views
Advanced Higher Computing Based on Heriot-Watt University Scholar Materials. File Handling. Lesson Objectives. Sequential Files & File Operations (create,open, close, input, append, delete). File Types. Files hold data and are held permanently on backing storage in three formats:.
E N D
Advanced Higher ComputingBased on Heriot-Watt University Scholar Materials File Handling Alford Academy Business Education and Computing
Lesson Objectives Sequential Files & File Operations (create,open, close, input, append, delete) Alford Academy Business Education and Computing
File Types Files hold data and are held permanently on backing storage in three formats: 1. sequential 2. random 2. binary There are five basic file handling operations that apply to all file types:- 1. create 2. open 3. close 4. insert 5. append Alford Academy Business Education and Computing
“ T O D “ A . “ , “ 2 2 5 3 6 2 5 “ EOF Sequential Files These are only used to store text. Example applications that create sequential files are Windows Notepad which creates simple text files. They are read from beginning to end and so cannot be read and written to at the same time. Can be thought of as a 1D array with each location storing one byte of information Also store hidden ASCII control codes: This symbol represent a newline This marks the end of the file EOF Alford Academy Business Education and Computing
File Manipulation - Open Alford Academy Business Education and Computing
Creating a sequential file • Algorithm • Enter filename • Open file for writing • Input information (entered into a text box) • Save to file • Close file • VB Code • Filename = Inputbox(“Please enter a file name”) • Open Filename for Output as #1 • 4. Print #1, Text1.Text • 5. Close #1 Alford Academy Business Education and Computing
Practice creating a sequential file 1. Create a folder for your Topic 5 VB File Handling programs 2. Use the code on page 104 of Scholar to create a sequential file and save the contents you type into a text box into the file (make sure Multiline is true for the text box object) – you will need to give the full pathname of the file when prompted for its name– see me! 3. Check that the file has been created – should exist in the folder you have specified. Open it with Notepad to check the contents. 4. Investigate what happens to the contents of the file when you run the program again and enter different data into the textbox. Alford Academy Business Education and Computing
Practice appending to a sequential file 1. Change the Open .. For Output to Open .. For Append 2. Investigate what happens to the contents of the file when you run the program again and enter different data into the textbox. Alford Academy Business Education and Computing
Practice reading from a sequential file 1. Use the code on page 105 of Scholar to read from a text file – you will need to ensure that the file exists (use the one you created previously) 2. Make sure that you have set the Multiline option to true on the Text1 text box 3. If your program works correctly then you should see the text in the file displayed in the Text1 text box Alford Academy Business Education and Computing
Practice deleting a file and putting all file operations together 1. Use the screen design on page 107 of Scholar to put all the separate file operations together – make sure you also have an APPEND button 2. Test your program works for any button clicked. 3. Print the code and the screen design for your folder of work. 4. Do the exercise on Page 108 of Scholar, using the code on Page 109 – to save you typing you may wish to copy and paste from the Scholar website!! 5. HOMEWORK – Answer Review questions 1-3 on page 111 of Scholar booklet Alford Academy Business Education and Computing