Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.

Similar presentations


Presentation on theme: "Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory."— Presentation transcript:

1 Reading and Writing Files Keeping Data

2 Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.

3 Sequential Access Files ä Think of files as being stored on tape. ä Sequential Access means you must pass over all the information in a file to get the stuff you want.

4 Illustration F1F2F4F3File 5F6File 7 To get from here...To here... We have to read all this. Read Head Tape Moves

5 Random Access Files ä Think of files stored on a disk. ä Random Access means you can have any information at any time. (Like in RAM!)

6 Illustration File One File Two Move the head from one file to another ignoring everything in between.

7 Random Access Files, Details ä WE WILL NOT COVER THESE IN THIS CLASS ä These files use indexes to locate information within them. ä It means dealing with the indexes, how the disk memory is used, special ways to delete files …..

8 Opening Files ä Questions to Answer: ä Do you want to read from it, write to it, both? ä Does the file already exist? ä What shall we do with the file that already exists? ä Are we looking at binary data or text data? ä Where is the file?

9 File Opening ä A file must first be opened before it can be used. ä Opening a file does two things: ä Makes sure its OK to open the file ä Assigns an identifier to the file. ä Files stay open until we close them (or the program ends)

10 OK to Open File? ä You haven’t opened it already ä You aren’t violating the file’s integrity ä Somebody else has it open… ä You’re already using for a different purpose.

11 Assigning an Identifier ä Allows us to identify which file to write to and read from. ä Useful if Multiple Files are Open

12 The Open Statement ä Open file For Mode As filenumber ä file is the Full pathname of the file to be opened. ä Mode is how the file will be used. ä filenumber is the number that identifies the file.

13 Modes ä Input ä Sequential access for reading, starts at beginning ä Output ä Sequential access for writing, erases old file and starts at beginning ä Append ä Sequential access - doesn’t erase the file, starts writing at the end.

14 Two Modes we Won’t Use ä Random ä Which we won’t use in this class - This is the default! ä Binary ä Which we also won’t use. Opens binary files.

15 The Open Statement, Con’t ä Opening a non-existent file for Input causes an Error! ä We’ll write a special function for handling this. ä Opening a non-existent file for Output causes it to be created. ä Opening an existing file for Output causes it to be erased. ä Opening an existing file for Append causes data to be entered at the end of the existing file.

16 Output to a File ä Write # filenum, variablelist ä filenum is the file identifying number ä variable list is a list of variables. ä This is the easiest file output function. ä Use it unless you are forced to use something else.

17 Format ä These are written out using commas between values and double quotes around strings. ä “Here is a string”,19,27.3,”String” ä “More strings”,20,15.2,”More Text”

18 Illustration of File Writing Dim fleeb As String, norb As Single Dim ski As Integer, i As Integer fleeb = “Test1” norb = 98.6 ski = 100 Open “file.txt” For Output As 1 Write #1, fleeb, norb, ski Close 1 “Test1”,98.6,100 file.txt

19 Printing to a File ä Print # filenum, var ; var, var ; var ä filenumber is from the Open Statement ä var can be ä the function Spc(n), for n spaces, ä Tab(n) for nth column tab, ä a variable ä the semicolon puts no space between items ä the comma prints items with spaces.

20 Input From a File ä Input # filenumber, variablelist ä filenumber is the file to read from ä Identified in the open statement. ä variables list is the list of variables to read the value into. ä numbers get numbers, strings get either double quoted strings or first word. ä Use this if at all possible.

21 Illustration of File Reading Dim fleeb(1 To 3) As String, Dim norb(1 To 3) As Single Dim ski(1 To 3) As Integer Dim i As Integer Open “file.txt” For Input As 1 For i = 1 to 3 Input #1, fleeb(i), norb(i),ski(i) Input #1, fleeb(i), norb(i),ski(i) Next i Close 1 “Test1”,98.6,100 “Test 2”,93.1,100 “Quiz 3”,18,25 file.txt fleeb norb ski

22 Input From a File, Con’t ä Input (n,filenumber) ä n is the number of characters to read in. ä filenumber is the file to read from. ä This was identified in the Open statement. ä This is useful if you must read in a file created with the Print# function.

23 Close File Statement ä Close filenumber(s) ä File identification number or numbers. ä Closes file and removes number assignment to it. ä Will “rewind” sequential files to beginning.

24 For i = 1 to 10 Open “f” for Input As 1 Input#, a(i), b(i) Close 1 Next i Good Thing Bad Thing Closing a File Open “f” for Input As 1 For i = 1 to 10 Input#, a(i), b(i) Next i Close 1

25 Formatting Data ä Write Format must be the same as Read Format to ensure that we have data integrity. ä Be very careful of this.

26 File Length ä We don’t always know the file length ä We can use the EOF(filenumber) function. ä filenumber is the number you assigned to the file. ä EOF returns True at the File end. ä EOF returns False otherwise.

27 Reading an unknown length file. Open “file.txt” For Input As 1 While(Not EOF(1)) Input#1, fred, wilma Input#1, fred, wilmaWend Close 1

28 FreeFile Function ä A useful function when you’re opening a lot of files and you’re not sure what number you want to assign to each. ä Identifies a number which has not yet been assigned to a file.

29 Input Files that Don’t Exist ä Requires us to use the error handling VB provides. ä We’ll put this in a function so that it doesn’t cause problems. ä When an Error occurs in VB it looks for an error handler. If none is found, the program ends. (Bad Thing)

30 Using the Error Handler ä We can create an error handler by inserting the statement: ä On Error Goto label ä Where label is a word that occurs later in the code with a comma after it. ä Special rules apply to these error handlers.

31 Comments ä We’ll tuck our error handler away in a function so that its use will be easy to control. ä This function will determine if a file exists. ä If it does it returns true ä Otherwise it returns false.

32 File_Exists Function Function File_Exists (FileName as String) As Integer On Error Goto File_error Open FileName For Input As 1 Open FileName For Input As 1 Close 1‘If it reaches this line Close 1‘If it reaches this line File_Exists = True‘the file was found File_Exists = True‘the file was found Exit Function Exit Function File_error:‘If it wasn’t found, control is File_Exists = False‘here via the On Error line. File_Exists = False‘here via the On Error line. Exit Function Exit Function End Function


Download ppt "Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory."

Similar presentations


Ads by Google