1 / 32

Reading and Writing Files

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. Sequential Access Files. Think of files as being stored on tape.

jihan
Download Presentation

Reading and Writing Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 To get from here... To here... Read Head F1 F2 F3 F4 File 5 F6 File 7 Tape Moves We have to read all this.

  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 Move the head from one file to another ignoring everything in between. File One File Two

  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 file.txt “Test1”,98.6,100

  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 file.txt 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) Next i Close 1 “Test1”,98.6,100 “Test 2”,93.1,100 “Quiz 3”,18,25 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. Closing a File Good Thing Bad Thing Open “f” for Input As 1 For i = 1 to 10 Input#, a(i), b(i) Next i Close 1 For i = 1 to 10 Open “f” for Input As 1 Input#, a(i), b(i) Close 1 Next i

  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 Wend 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 Close 1 ‘If it reaches this line File_Exists = True ‘the file was found Exit Function File_error: ‘If it wasn’t found, control is File_Exists = False ‘here via the On Error line. Exit Function End Function

More Related