Filehandling
-
Upload
amandeep-kaur -
Category
Education
-
view
1.165 -
download
0
description
Transcript of Filehandling
Presentationon
File Handling
Contents
Definition Of File
Types Of Files
Operations on File
Opening A File
Closing A File
Writing Data To A File
Reading Data From A File
Detecting The End of File
Storing A File
Demo Application
Definition Of File
A File is a collection of data stored on a
computer’s disk.
Information can be saved to files and later
reused.
Types Of Files
There are three types of files.
1) Sequential Access Files
2) Random Access Files
3) Binary Files
Sequential Access File
A sequential access file is a stream of data that
must be read from its beginning to its end.
To read a record that is stored in the middle or at
the end of sequential access file an application
must read all the records in the file before it.
Random Access File
In Random access file Records must be identical
in length and may be accessed in any order.
An application may immediately jump to any
record in a random access file without first
reading the preceding records.
Binary File
In binary file data is stored in binary form not in
plain text.
The records in binary files may vary in length.
Binary files are usually smaller than text files but
user cannot view their contents with text editor.
Binary File
In binary file data is stored in binary form not in
plain text.
The records in binary files may vary in length.
Binary files are usually smaller than text files but
user cannot view their contents with text editor.
Operations on File
File Open Mode
Input Used with sequential acess file. Data will read from the files.
Output Used with sequential access files .Data will be written to the file.
Append Used with a sequential access file. Data will be written to the end of the file.
Random This file is opened for random access . If the file does not exist,it is created.
Binary The file is opened in binary mode. If the file does not exist , it is created .
Opening A File
Open statement is used to open a file.
Open*filename*for mode as # filenumber [len=recordlength]
Syntax :
Contd..
When a sequential file is opened , visual basic
creates a file buffer.
A file buffer is a small holding section of memory
that data is first written to.
Closing A File
Close statement is used to close a file.
Close [filenumberlist]
Syntax :
• In syntax, Filenumberlsit is one or more file numbers separated by commas.
Contd..
The # symbol must appear with each file number.
Example close #1,#2
If user don’t specify any file number , visual basic
closes all the files the applications has opened.
Writing data to a File
Data may be written to a file that is opened in
either output or append mode.
The write statement is used to write a data to the
file.
Write # filenumber, [itemlist]
Syntax
Reading data from a file
Data may be read from a file that already exist
and is opened in input mode.
The input statement is used to read data from a
file.
Input # filenumber, itemlist
Syntax
Opening a file in append mode
When a file is opened in append mode data is
written to the end of the file.
If the file is already exist its contents are not
erased.
If the file does not exist it is created.
Contd..
.
Open filename for append as #1
Syntax
Detecting the end of the file
The EOF function detects when the end of file has been
reached.
The function proves true if the end of file has been
reached or false if the end of file has not been reached.
EOF(file number)
Syntax
Application on file handling
How to apply picture on command button
Select the command button.
Draw it and then go to properties window.
Change the style property to 1-graphical.
Select the picture option.
Select the picture user want to apply.
How to draw a shape
Shape option
•Select the shape option from the toolbox.
•Then go to shape properties and select any
option.
Coding Of this Form
Private Sub cmdappend_Click()
Unload Me
frmappend.Show
End Sub
Private Sub cmdclose_Click()
Unload Me
End
End Sub
Contd…. Private Sub cmdcreate_Click()
Unload Me
frmcreate.Show
End Sub
Private Sub cmdread_Click()
Unload Me
frmread.Show
End Sub
Private Sub cmdwrite_Click()
Unload Me
frmwrite.Show
End Sub
Contd….
Dim fname As String
Dim productname As String
Dim companyname As String
Dim price As Integer
Dim intcount As Integer
Private Sub cmdclose_Click()
Unload Me
End
End Sub
Contd…. Private Sub cmdcreate_Click()
fname = InputBox("Enter the File Name")
Open fname For Output As #1
For intcount = 1 To 4
MsgBox " Enter the product details " & FormatNumber(intcount, 0)
productname = InputBox("Enter the Product name")
companyname = InputBox("Enter the Company name")
price = Val(InputBox("Enter the Price"))
Write #1, productname, companyname, price
Next intcount
Close #1
End Sub
Control Array
A control array is a group of controls all of the
same type.
The control array has a name but the individual
controls in the array donot.
To refer to a member of a control array, the
syntax is:
ControlName(Index)[.Property]
How to create a control array
Select (command button,text box ,label etc).
Then draw it.
Then copy it and paste it.
It will ask do you want to create a control array
then click yes.
CodingPrivate Sub cmdappend_Click()Open "d:\aman.txt" For Append As #1productname = "laptop"companyname = "samsung"price = 33000Write #1, productname, companyname, priceClose #1End Sub
Private Sub cmdclose_Click()Unload MeEndEnd Sub
CodingPrivate Sub cmdclose_Click()Unload MeEndEnd Sub
Private Sub cmdwrite_Click()Open "d:\aman.txt" For Output As #1productname = "laptop"companyname = "samsung"price = 33000Write #1, productname, companyname, priceClose #1End Sub
Thank You !!!!!!!!