|
-
May 14th, 2001, 09:36 AM
#1
Blocks of Stuff in a text file
I haven't gotten too into using text files in my programs, but finaly I need to do something rather complicated with them. My question is this:
If I have a huge text file, how can I get to certain parts of the text file? I would like to deal with just a certain block of information in a file rather than lots of little files. It seems a little impractical to load the whole file into a string, then chop off the ends I don't need. Anyone understand my question? Thanks.
-
May 14th, 2001, 09:44 AM
#2
Re: Blocks of Stuff in a text file
May be random access file may help you. Check Open file for random in MSDN. Another try is: ini file and related Api to set and retrieve values (writeprivateprofilestring and getprivateprofilestring and others).
Or you can switch to a little DB maybe with access (check both DAO and Ado for this).
However, this will mean to rewrite your fies, and - I am afraid - a pice of your application...
Hope it helped,
Cesare Imperiali
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
May 14th, 2001, 09:48 AM
#3
Re: Blocks of Stuff in a text file
Yes, I'd figured I'd probably have to go with random, but how do I do it? I would really appreciate it if anyone could maybe explain how I would use it. Or if anyone knows of a good tutorial somewhere, that'd be great too!
-
May 14th, 2001, 09:49 AM
#4
Re: Blocks of Stuff in a text file
Dim InputData
Open "MYFILE" for input as #1 ' Open file for input.
Do While Not EOF(1) ' Check for end of file.
Line input #1, InputData ' Read line of data.
' Do whatever processing you need to do
Loop
Close #1 ' Close file.
-
May 14th, 2001, 09:53 AM
#5
Re: Blocks of Stuff in a text file
Right, now if I wanted just a certain part of the file I opened, what would I do?
-
May 14th, 2001, 10:40 AM
#6
Re: Blocks of Stuff in a text file
this comes from msdn (january 2001)
Using Random File Access
The File System Object model does not provide random file creation or access methods. If you need to create or read random files, this information will help you do so.
The bytes in random-access files form identical records, each containing one or more fields. A record with one field corresponds to any standard type, such as an integer or fixed-length string. A record with more than one field corresponds to a user-defined type. For example, the Worker Type defined below creates 19-byte records that consist of three fields:
Type Worker
LastName As String * 10
Title As String * 7
Rank As String * 2
End Type
Declaring Variables
Before your application opens a file for random access, it should declare all variables required to handle data from the file. This includes user-defined types, which correspond to records in the file, as well as standard types for other variables that hold data related to processing a file opened for random access.
Defining Record Types
Before opening a file for random access, define a type that corresponds to the records the file does or will contain. For example, an Employee Records file could declare a user-defined data type called Person as follows:
Type Person
ID As Integer
MonthlySalary As Currency
LastReviewDate As Long
FirstName As String * 15
LastName As String * 15
Title As String * 15
ReviewComments As String * 150
End Type
Declaring Field Variables in a Type Definition
Because all records in a random-access file must have the same length, it is often useful for string elements in a user-defined type to have a fixed length, as shown in the Person type declaration above, where, for instance, FirstName and LastName have a fixed length of 15 characters.
If the actual string contains fewer characters than the fixed length of the string element to which it is written, Visual Basic fills the trailing spaces in the record with blanks (character code 32). Also, if the string is longer than the field size, it is truncated. If you use variable-length strings, the total size of any record stored with Put or retrieved with Get must not exceed the record length specified in the Open statement’s Len clause.
Declaring Other Variables
After defining a type that corresponds to a typical record, declare any other variables that your application needs to process a file opened for random access. For example:
' A record variable.
Public Employee As Person
' Tracks the current record.
Public Position As Long
' The number of the last record in the file.
Public LastRecord As Long
Opening Files for Random Access
To open a file for random access, use the following syntax for the Open statement:
Open pathname [For Random] As filenumber Len = reclength
Because Random is the default access type, the For Random keywords are optional.
The expression Len = reclength specifies the size of each record in bytes. Note that every string variable in Visual Basic stores a Unicode string and that you must specify the byte length of that Unicode string. If reclength is less than the actual length of the record written to the file, an error is generated. If reclength is greater than the actual length of the record, the record is written, although some disk space may be wasted.
You could use the following code to open a file:
Dim FileNum As Integer, RecLength As Long, Employee As Person
' Calculate the length of each record.
RecLength = LenB(Employee)
' Get the next available file number.
FileNum = FreeFile
' Open the new file with the Open statement.
Open "MYFILE.FIL" For Random As FileNum Len = RecLength
Editing Files Opened for Random Access
If you want to edit a random access file, first read records from the file into program variables, then change the values in the variables, and finally, write the variables back into the file. The following sections discuss how to edit files opened for random access.
Reading Records into Variables
Use the Get statement to copy records into variables. For instance, to copy a record from the Employee Records file into the Employee variable, you could use the following code:
Get FileNum, Position, Employee
In this line of code, FileNum contains the number that the Open statement used to open the file; Position contains the record number of the record to copy; and Employee, declared as user-defined type Person, receives the contents of the record.
Writing Variables to Records
Use the Put statement to add or replace records into files opened for random access.
Replacing Records
To replace records, use a Put statement, specifying the position of the record you want to replace; for example:
Put #FileNum, Position, Employee
This code will replace the record number specified by Position, with the data in the Employee variable.
Adding Records
To add new records to the end of a file opened for random access, use the Put statement shown in the preceding code fragment. Set the value of the Position variable equal to one more than the number of records in the file. For example, to add a record to a file that contains five records, set Position equal to 6.
The following statement adds a record to the end of the file:
LastRecord = LastRecord + 1
Put #FileNum, LastRecord, Employee
Deleting Records
You could delete a record by clearing its fields, but the record would still exist in the file. Usually you don’t want empty records in your file, because they waste space and interfere with sequential operations. It is better to copy the remaining records to a new file, and then delete the old file.
To remove a deleted record in a random-access file
Create a new file.
Copy all the valid records from the original file into the new file.
Close the original file and use the Kill statement to delete it.
Use the Name statement to rename the new file with the name of the original file.
For More Information For additional information on random file access, see "Open Statement."
The following example opens the file in Random mode. The file contains records of the user-defined type Record.
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
Dim MyRecord As Record ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' Close before reopening in another mode.
Close #1
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|