13 01 DO3BVDSG2ZAEVQBAMYUEMDK4V3JML4RLG33LEKA




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Working With Files
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Chapter 13Working With Files


Reading and writing disk files is an essential part of most programs. Visual Basic has a full set of file access tools.
Files here, files there, files everywhere. The only thing that’s been growing faster than hard-disk sizes, it seems, is the number of files you have to store on them. Almost every program you write will need to deal with files, and the way your program handles this challenge has a big impact on its overall quality. Poor design and implementation of file access and management can have a variety of deleterious effects on your program—ranging from slow performance to that ultimate no-no, loss of data.
Note that I draw a distinction between file access and file management. File access encompasses all the procedures for reading data from and writing data to files, while file management refers to such tasks as creating folders, moving and deleting files, and so on. We’ll look at file access in this chapter and file management in the next, covering not only Visual Basic statements but also the controls and objects designed for file-related tasks.
Visual Basic file access can be done in two ways: the traditional methods, which use statements that have been part of the Basic language since the beginning; or the object-oriented method (new with the current version of Visual Basic), which treats the file system as a file system object (FSO) that has properties and methods. At present, the FSO approach is not as complete as the traditional methods and is applicable mainly to file management tasks (as covered in Chapter 14). As for file access, the FSO model can be used only to read and write text files, as I’ll explain later in this chapter. Note that you can mix traditional and FSO methods in the same program.
Reading And Writing Files
If you want to save data until the next time you use your computer, you have to store it in a disk file. If you want to transfer data to another computer using a diskette, you must store it in a disk file. If you want to use data created by another program, you must read it from a disk file. Sure, these rules have a few exceptions, but the general message is clear: Knowing how to use disk files is essential. The Basic language offers a variety of statements and functions that provide complete flexibility in file access. While these program statements and functions are part of the Basic language, they are not directly related to the Visual Basic user interface or to its objects and events. The file manipulation statements will be located in general procedures, or very rarely, in event procedures.

Visual Basic file access consists of three steps:

1.  Open the file.
2.  Read data from or write data to the file.
3.  Close the file.

Sounds relatively simple, but within these three steps hides a multitude of options and pitfalls. Let’s start by looking at the three different kinds of file access offered by Visual Basic.

Types Of File Access
All file data, no matter what the source or contents, is stored as a sequence of bytes in the file. Depending on the type of data and the needs of the program, you will choose from one of three ways of accessing those bytes. The type of file access your program uses ultimately determines the methods it can use to read and write data, as well as the way the data is interpreted. Each of the three access methods is appropriate for certain types of data. This section describes each type briefly, with full details given later in the chapter.

Sequential Access Files
A sequential access file stores data as a series of variable-length records. The term record simply means a unit of data. For example, if you store the value of a type Integer variable in a file, that’s one record. Likewise, the contents of a String variable would be another record. The records are variable length, because each record’s length is determined by its contents—not by some overall property of the file. In other words, a sequential access file contains records of different lengths.
The data in a sequential file is assumed to be text, meaning that each byte in the file represents a character. The term sequential indicates that this type of file must always be read from the beginning. To read data in the middle of the file, you must first read all the preceding data in sequence. There is no easy way to jump directly to the data in the middle of the file. In a sequential file, numeric data is stored as its corresponding characters, and string data is stored in quotation marks. For example, the value 123 is stored as the string “123”—not as a binary representation of the value 123.
In a sequential file, each record is a line of text terminated with a carriage return-line feed (CR-LF) character. Each record can contain zero or more characters, and the records in a given file can be of different lengths. You treat each record as a single line of text, which is appropriate for manipulating plain text files. You can also treat sequential file records as groups of one or more fields separated from each other by delimiters. This approach is useful for working with data that is divided into units that differ in length.
Random Access Files
A random access file stores data as a series of fixed-size records, with each record the same size as all the others in the file. The records in a random access file are numbered sequentially starting at one. Random access files allow a program to directly access each record by its number, without having to access previous records first. In a 2,000-record file, for example, you could access record 1,199 without first having to read records 1 through 1,198. You can store both text and numbers in a random access file. Text is stored as characters, whereas numbers are stored in a special binary format.

Each record in a random access file consists of a fixed number of bytes, or characters. Each record contains one or more fields, each of which also has a fixed length. It follows that the record size is equal to the sum of the field sizes. The sizes of a file’s records and fields are defined by the program when the file is created. Unlike sequential files, random files do not use delimiters to separate fields and records. The fixed lengths of the fields and records are used to determine where each field and record begins and ends. It is this fixed record size that permits random access to data in the file. For example, if the record size is 100 bytes, you know that the 55th record in the file begins at byte position 5,401.
Binary Access Files
Binary access files store data as an unformatted sequence of bytes. No record lengths or delimiter characters are used to provide structure to the file data. Binary access allows us to manipulate the individual bytes of a file without any assumptions about what the bytes represent. Unlike random access and sequential access files, binary access files are not limited to ASCII text or Basic variables. Binary access can be used to read and modify any kind of disk file, as well as to store program data. Because a binary file has no records or other structures, however, the program is completely responsible for keeping track of what is stored where.




Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.



Wyszukiwarka

Podobne podstrony:
s V 13 01 28 wyniki 02 2013
13 01
13 01
Analiza Wykład 12 (13 01 11)
Analiza Wykład 12 (13 01 11)
KNR 13 01
Analiza Finansowa Wykład 07 13 01 10
odpowiedzi do egzaminu 13 01 2009
13 01 Palowanie
FM wyklad 11 13 01 2011
pdm 2015 01 13
1 14 Rynek reklamy 01 13

więcej podobnych podstron