files vb6 net

Download Files Vb6 Net

Post on 04-Jun-2018

221 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 8/13/2019 Files Vb6 Net

    1/21

    VB6 - FILES (Processing a random "file of records")

    Write, read, re-write to a Direct (Random) file of records. See also READ, WRITE for simle files.

    Module: modFile

    P!lic T#e empRecord $ Define !ser-definedt#e.

    ID As Integer %ame As String & 'End T#e

    Form: frmFile

    Private Sub cmdClearClic!"# ist*.+learEnd S!

    Private Sub cmdMa!eFileClic!"#$ Declare ariales.Dim m#Record As emRecordDim Record%!mer As Integer

    $ en file for random access.en "c/TEST0IE.t1t" 0or Random 2

    As 3* en 4 en(m#Record)

    $P!t some d!mm# data into t5e file0or Record%!mer 4 * To 6 $ oo 6 times. m#Record.ID 4 Record%!mer $ Define ID. m#Record.%ame 4 "7#%ame" 8 2

    Record%!mer $ +reate a string. $ Write record to file. P!t 3*, Record%!mer, m#Record

    $ S5ow t5e record into t5e listo1 ist*.AddItem m#Record.ID 8 " " 8 2

    m#Record.%ame%e1t Record%!mer+lose 3* $ +lose file.

    End S!

    Private Sub cmdRa$domReadClic!"#n Error 9oTo Prolems Dim m#Record As emRecord Position 4 :al(t1tPosition)

    If Position ; 0(*) < en(m#Record) T5en 9oTo Prolems End If

    9et 3*, Position, m#Record $ Read t5irdrecord. t1t%ame 4 m#Record.%ame 9oTo Adios

    Prolems 7sg=o1 "=ad Inde1 - tr# Again"AdiosEnd S!

    Private Sub cmdReadFileClic!"#$ Declare ariales.

    Dim m#Record As emRecordDim Position, totalRecords, #tes0ile,#tesRecord$ en samle file for random access.en "c/TEST0IE.t1t" 0or Random 2 As 3* en 4 en(m#Record)$find o!t 5ow man# records are t5ere in file 3*#tes0ile 4 0(*)#tesRecord 4 en(m#Record)totalRecords 4 #tes0ile < #tesRecord

    $ read data (randoml#)from last to first rec.0or Position 4 totalRecords To * Ste -* $ Read t5e samle file !sing t5e 9et.

    9et 3*, Position, m#Record $ Read t5e record $ S5ow t5e record into t5e listo1 ist*.AddItem m#Record.ID 8 " " 8 2 m#Record.%ame %e1t $+lose 3* $ +lose t5e actie file.End S!

    Private Sub FormLoad"#n Error 9oTo Adios$delete an# reio!s ersion of t5e file

    >ill "c/TEST0IE.t1t"AdiosEnd S!

  • 8/13/2019 Files Vb6 Net

    2/21

    VB%&E' ver(io$

    empRecord Cla((

    P!licStr!ct!reemRecord$ Define !ser-defined t#e. DimID AsS5ort )VBFi*edStri$+",#. Dim%ame AsStringEndStr!ct!re

    frmFile Cla(( "/I0#

    PriateS!cmdClearClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andlescmd+lear.+lic ist*.Items.+lear()EndS!

    PriateS!cmdMa!eFileClic!(=#:aleentSenderAsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs) @andlescmd7ae0ile.+lic

    $ Declare ariales.Dimm#Record AsemRecordDimRecord%!mer AsS5ortDimrecen AsInteger$ en file for random access.recen 4 en(m#Record)

    0ileen(*, "c/TEST0IE.t1t",en7ode.Random, enAccess.Write,enS5are.ocWrite, recen)

    $P!t some d!mm# data into t5e file0orRecord%!mer 4 * To6 $ oo 6 times. $ Define ID. m#Record.ID 4 Record%!mer

    $ +reate a string. m#Record.%ame 4 "Data-Emlo#ee-" 8

    Record%!mer$ Write record to file.0ileP!t(*, m#Record, Record%!mer)$ S5ow t5e record into t5e listo1

    ist*.Items.Add(m#Record.ID 8 2 " " 8 m#Record.%ame)%e1tRecord%!mer

    0ile+lose(*) $ +lose file.

    $enale t5e ot5er command !ttons cmdRead0ile.Enaled 4 Tr!e 7e.cmdRandomRead.Enaled 4 Tr!eEndS!

    PriateS!cmdRa$domReadClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs) @andlescmdRandomRead.+lic Dimm#Record AsemRecord DimPosition AsS5ort Dimfile%!m AsInteger $ en file (if needed) for random access. nErrorRes!me%e1t file%!m 4 0ree0ile() 0ileen(file%!m, "c/TEST0IE.t1t",

    en7ode.Random, , , en(m#Record))

    nError9oToProlems Position 4 :al(t1tPosition.Te1t) IfPosition ; 0(file%!m) < en(m#Record) T5en 9oToProlems EndIf 0ile9et(file%!m, m#Record, Position)

    t1t%ame.Te1t 4 m#Record.%ame 9oToAdiosProlems 7sg=o1("=ad Inde1 - tr# Again")Adios 0ile+lose(file%!m) EndS!

    PriateS!cmdReadFileClic!(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andles cmdRead0ile.+lic $ Declare ariales. Dimm#Record AsemRecord Dim#tes0ile, Position, AsS5ort DimtotalRecords, recen AsS5ort Dimfile%!m AsInteger

    $ en samle file for random access. file%!m 4 0ree0ile() recen 4 en(m#Record) 0ileen(file%!m, "c/TEST0IE.t1t",

    en7ode.Random, , , recen) $find o!t 5ow man# records are t5ere in file 3* #tes0ile 4 0(file%!m) totalRecords 4 #tes0ile < recen $ read data from last rec. to first rec. 0orPosition 4 totalRecords To* Ste-* 0ile9et(file%!m, m#Record, Position)

    $ S5ow t5e record into t5e listo1 ist*.Items.Add(m#Record.ID 8 " "

    8 m#Record.%ame)%e1t

    0ile+lose(file%!m) $ +lose file. EndS!

    PriateS!frmFileLoad(=#:aleentSender AsS#stem.?ect, =#:aleentArgs AsS#stem.EentArgs)@andles7#=ase.oad nError9oToAdios

    >ill("c/TEST0IE.t1t")

    AdiosEndS!

  • 8/13/2019 Files Vb6 Net

    3/21

    VB6 S1$ta*% /E' 2 P0' S'3'EME&'

    Readse#word secif#ing t5e oerations ermitted on t5e oen file

    # ot5er rocesses Sared, Loc! Read, Loc! 7rite, and Loc!

    Read 7rite.filen!mer Re!ired. A alid file n!mer in t5e range * to H**, incl!sie. se t5e

    FreeFilef!nction to otain t5e ne1t aailale file n!mer.reclengt5 tional. %!mer less t5an or e!al to J',6K6 (#tes). 0or files

    oened for random access, t5is al!e is t5e record lengt5. 0orse!ential files, t5is al!e is t5e n!mer of c5aracters !ffered.

    V. Matos - Input/Output VB.NET Page 3

  • 8/13/2019 Files Vb6 Net

    4/21

    Visual Basic .NET I/OT5ese e1certs are taen

    from t5e

    L7icrosoft .%ET

    @el 0acilit#.

    File Access Types, Functions, and Statements

    =# itself, a file is not5ing more t5an a series of related #tes of data on a dis. W5en #o!r alication accesses afile, it m!st ass!me w5et5er t5e #tes reresent c5aracters, data records, integers, strings, and so on. o! tell#o!r alication w5at to ass!me # secif#ing t5e file$s access t#e.

    T5e file access t#e #o! !se deends on w5at ind of data t5e file contains. :is!al =asic roides t5ree t#es of fileaccess

    Se!ential, for reading and writing te1t files in contin!o!s locs.

    Random, for reading and writing te1t or inar# files str!ct!red as fi1ed-lengt5 records.

    =inar#, for reading and writing aritraril# str!ct!red files.

    Sequential accessis designed for !se wit5 lain te1t files. Eac5 c5aracter in t5e file is ass!med to reresent eit5er

    a te1t c5aracter or a te1t-formatting se!ence, s!c5 as a newline c5aracter. Data is stored as A%SI c5aracters.

    A file oened for random accessis ass!med to e comosed of a set of identical-lengt5 records, or sets of fieldscontaining information. o! can emlo# !ser-defined t#es to create records made ! of n!mero!s fields, eac5 ofw5ic5 can 5ae different data t#es. Data is stored as inar# information.

    Binary accessallows #o! to !se files to store data in an# form t5at s!its #o!r needs, w5et5er it is n!meric, string,or ot5. =inar# access is similar to random access e1cet t5at no ass!mtions are made ao!t data t#e or recordlengt5. @oweer, #o! m!st now recisel# 5ow t5e data was written to t5e file to retriee it correctl#. 0or e1amle,if #o! store a series of names and 5one n!mers, #o! need to rememer t5at t5e first field (t5e name) is te1t and

    t5e second field (t5e 5one n!mer) is n!meric.

    File 3cce(( Fu$ctio$( a$d Stateme$t(

    T5e following f!nctions are !sed wit5 se!ential, random, and inar# file access.

    Dir 0!nction E0 0!nction 0ile+o# 0!nction

    0ileDateTime 0!nction 0ileen 0!nction 0ree0ile 0!nction

    9etAttr 0!nction oc 0!nction 0 0!nction

    See 0!nction SetAttr 0!nction

    Woring wit5 large amo!nts of data often re!ires t5at data e written to or read from a file. =efore eit5eroeration can tae lace, 5oweer, t5e file m!st first e oened. T5e 0ileen 0!nction lets #o! create and accessfiles wit5 one of t5ree t#es of file access

    Se!ential access (I$put, 4utput, and 3ppe$dmodes) is !sed for writing te1t files, s!c5 as error logs

    and reorts. Random access (Ra$dommode) is !sed to read and write data to a file wit5o!t closing it. Random-access

    files ee data in records, w5ic5 maes it eas# to locate information !icl#.

    =inar# access (Bi$ar1mode) is !sed to read or write to an# #te osition in a file, s!c5 as storing or

    disla#ing a itma image.

    Do not !se t5e File4pe$f!nction to mani!late files t5at are !s!all# edited # a secific alication, s!c5as 7icrosoft ffice doc!ments or registr# files. Doing so will ca!se file corr!tion and loss of file integrit#.

    V. Matos - Input/Output VB.NET Page 4

  • 8/13/2019 Files Vb6 Net

    5/21

    T5e following tale s5ows t5e statements t#icall# !sed w5en writing data to and reading it from files.

    3cce(( t1pe 7riti$+ data Readi$+ data

    Se!ential Print, Printine 0!nctions In!tString 0!nction

    Random 0ileP!t 0!nction 0ile9et 0!nction

    =inar# 0ileP!t 0!nction 0ile9et 0!nction

    FileOpen Function

    ens a file for in!t or o!t!t.

    Public Sub File4pe$" B1ValFileNumber3( I$te+er5 B1ValFileName3( Stri$+5

    B1Val Mode3( 4pe$Mode5 4ptio$al B1ValAccess3( 4pe$3cce(( 8 4pe$3cce((%Default5 4ptio$al B1ValShare3( 4pe$Sare 8 4pe$Sare%Default5 4ptio$al B1ValRecordLength3( I$te+er 8 -9 #

    Parameter(

    FileNumberRe!ired. An# alid file n!mer. se t5e FreeFilef!nction to otain t5e ne1t aailale file n!mer.

    FileNameRe!ired. Stri$+e1ression t5at secifies a file name M ma# incl!de director# or folder, and drie.

    ModeRe!ired. E$umsecif#ing t5e file mode 3ppe$d, Bi$ar1, I$put, 4utput, or Ra$dom.

    Accesstional. >e#word secif#ing t5e oerations ermitted on t5e oen file Read, 7rite, or Read7rite.

    Defa!lts to Read7rite.Share

    tional. E$umsecif#ing t5e oerations restricted on t5e oen file # ot5er rocesses Sared, Loc!Read, Loc! 7rite, and Loc! Read 7rite. Defa!lts to Sared.

    RecordLengtht