160 likes | 346 Vues
المحاضرة السادسة. التعامل مع الملفات Files in Visual Basic By Hitham M. Abo Bakr. In This Lecture. INTRODUCTION SEQUENTIAL FILE HANDLING SEQUENTIAL FILE HANDLING FUNCTIONS SEQUENTIAL FILE HANDLING EXAMPLES SOME FILE MANUPULATION FUNCTIONS. Introduction.
E N D
المحاضرة السادسة التعامل مع الملفات Files in Visual Basic By Hitham M. Abo Bakr
In This Lecture INTRODUCTION SEQUENTIAL FILE HANDLING SEQUENTIAL FILE HANDLING FUNCTIONS SEQUENTIAL FILE HANDLING EXAMPLES SOME FILE MANUPULATION FUNCTIONS
Introduction • As far as Visual Basic 6 is concerned, there are three modes in which a file canbe accessed. • Text Mode (Sequential Mode) • Random Access Mode • Binary Mode
Text Mode and Binary Mode In the Text Mode, data is ALWAYS written and retrieved as CHARACTERS. Hence, any number written in this mode will result in the ASCII Value of the number being stored. In the Binary Mode, everything is written and retrieved as a Number.Hence, The Number 17 Will be stored as [ 17 ] in this mode andcharacters will be represented by their ASCII Value as always.
Random Access Mode Just like the Binary Mode, the Random Access Mode allows us to gain instant access to any piece of information lying anywhere in the file at a cost.In this case, we must standardize each piece of information. Random Access Mode allows us to read or write data at a particular record position rather than a byte position like in Binary Mode.
SEQUENTIAL FILE HANDLING • To open a File in Sequential Mode, we need to use the Open Command like this: Open <FILENAME> For <MODE> As <FILE#> • FILENAME : Contains the Path of the File to be opened. • MODE : Can be INPUT, OUTPUT or APPEND • FILE# : Any number from 1 to 255 preceded by a #
Open File Mode INPUT -> Used to Read From a File.OUTPUT -> Used to Write to a File. (Remove Old file if Exist)APPEND -> Used to Write to a File. (Appensd on Old File if Exist)
Open and Close the File • to open a file Contacts.txt in Input Mode we use the Open Command like this: • Open "C:\Contacts.txt" For Input as #1 • Notice that the path of the file is mentioned. If the path is not mentioned, Visual BASIC assumes the file to be in the current directory. • To close the file we use the CLOSE Command like this • Close #1
Examples C:\Contacts.txt" Dim tmp as String Open "C:\Contacts.txt" For Input as #1 Line Input #1, tmp Close #1 Msgboxtmp "Sanchit",9811122233 "Friend",9812345634 "Enemy",9821630236 Dim tmp as String, contents as String Open "C:\Contacts.txt" For Input as #1 While Eof(1) = 0 line input #1,tmp contents = contents + tmp Wend Close #1 Msgbox contents the Message Box displays: "Sanchit",9811122233
Separate Two fields into Two Variables Input #1, strname, lngphone Msgbox "Name = "+strname Msgbox "Phone = "+Str(lngphone)
Writing In Sequential Files My Phone number is 12345678 910 "My Phone number is 12345678",910 We Use Either Write or print Open "C:\names_database.txt" For Output As #1 Print #1, "My Phone number is 12345678", 910 Write #1, "My Phone number is 12345678", 910 Close #1 End What is the difference between Write and Print Print saves the text without separators like “ ” for string or # # for Date and but write do that..
Examples See Examples
SEQUENTIAL FILE HANDLING FUNCTIONS FreeFile() : Returns an Integer representing the nextfile number available for use by the Open statement. EOF() : Returns anInteger containing the Boolean value True when the end of a file opened for sequential Input has been reached. FileLen() : Returns a Long specifying the length of a file in bytes. LOF() : Returns a Long representing the size, in bytes, of a file opened using the Open statement. Seek() : Returns a Long specifying the current read/write position within a file opened using the Open statement.
Some File Manipulation Functions • Copy Files using : • FilecopySourceFile, Destination File • To Delete a file • Kill Filename • To Rename File • Name Oldname As NewName • Create new Folder • MkDirDirName