Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.

Similar presentations


Presentation on theme: "Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling."— Presentation transcript:

1 Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling

2 Alford Academy Business Education and Computing2 Lesson Objectives Sequential Files & File Operations (create,open, close, input, append, delete)

3 Alford Academy Business Education and Computing 3 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

4 Alford Academy Business Education and Computing 4 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 “TOD“A.“,“2253625“ EOF Also store hidden ASCII control codes: This symbol represent a newline EOF This marks the end of the file

5 Alford Academy Business Education and Computing 5 File Manipulation - Open Opening a fileIf the file does not exist then it is created and then opened by the operating system which preserves a portion of memory for the file. VB Syntax Open “Filename” For Input as #1 Open “Filename” For Output as #1 Open “Filename” For Append as #1 For every file used in a program there needs to be separate # number eg #1, #2, #3 would refer to 3 separate files

6 Alford Academy Business Education and Computing 6 Creating a sequential file Algorithm 1.Enter filename 2.Open file for writing 3.Input information (entered into a text box) 4.Save to file 5.Close file VB Code 1.Filename = Inputbox(“Please enter a file name”) 2.Open Filename for Output as #1 4. Print #1, Text1.Text 5. Close #1

7 Alford Academy Business Education and Computing 7 Practice creating a sequential file 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! 4. Investigate what happens to the contents of the file when you run the program again and enter different data into the textbox. 3. Check that the file has been created – should exist in the folder you have specified. Open it with Notepad to check the contents. 1. Create a folder for your Topic 5 VB File Handling programs

8 Alford Academy Business Education and Computing 8 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.

9 Alford Academy Business Education and Computing 9 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) 3. If your program works correctly then you should see the text in the file displayed in the Text1 text box 2. Make sure that you have set the Multiline option to true on the Text1 text box

10 Alford Academy Business Education and Computing 10 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


Download ppt "Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling."

Similar presentations


Ads by Google