number of records in a random access file?
Author |
Message |
Lee Christi #1 / 6
|
 number of records in a random access file?
how can i retrive a count of the number of records in a file I created in my VB .NET app using the following code to inistrate what the heck im talking about; FileOpen(fileNum, "myTestFile.txt", OpenMode.Random, OpenAccess.Default, OpenShare.Default, ) FilePut(fileNum, testRecord, 1) FilePut(fileNum, testRecord, 2) FilePut(fileNum, testRecord, 3) etc... where testFile is a variable declaired as; dim testRecord as myStruct where myStruct is a structure which contains (amoung others) variable length strings and variable length arrays. Would be nice if i could do something like; numberOfRecords = FileRecordCount(fileNum) 'Made up code (not real) while i have the file open -Thankyou
|
Thu, 21 Oct 2004 23:07:54 GMT |
|
 |
Armin Zingle #2 / 6
|
 number of records in a random access file?
Quote: > how can i retrive a count of the number of records in a file I > created in my VB .NET app using the following code to inistrate > what the heck im talking about; > FileOpen(fileNum, "myTestFile.txt", OpenMode.Random, > OpenAccess.Default, OpenShare.Default, ) > FilePut(fileNum, testRecord, 1) > FilePut(fileNum, testRecord, 2) > FilePut(fileNum, testRecord, 3) > etc... > where testFile is a variable declaired as; > dim testRecord as myStruct > where myStruct is a structure which contains (amoung others) > variable length strings and variable length arrays.
Variable length strings in a structure are of no use for random files because random files have a fixed record length: ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vaconDeclaringVariablesForRandomFileA ccess.htm Quote: > Would be nice if i could do something like; > numberOfRecords = FileRecordCount(fileNum) 'Made up code (not > real) > while i have the file open
numberOfRecords = Lof(1) \ len(testRecord) Armin
|
Fri, 22 Oct 2004 00:43:41 GMT |
|
 |
Lee Christi #3 / 6
|
 number of records in a random access file?
Quote:
> > how can i retrive a count of the number of records in a file I > > created in my VB .NET app using the following code to inistrate > > what the heck im talking about; > > FileOpen(fileNum, "myTestFile.txt", OpenMode.Random, > > OpenAccess.Default, OpenShare.Default, ) > > FilePut(fileNum, testRecord, 1) > > FilePut(fileNum, testRecord, 2) > > FilePut(fileNum, testRecord, 3) > > etc... > > where testFile is a variable declaired as; > > dim testRecord as myStruct > > where myStruct is a structure which contains (amoung others) > > variable length strings and variable length arrays. > Variable length strings in a structure are of no use for random files > because random files have a fixed record length:
Well actually it seems to work you know. I can't understand how. Perhaps it it using a marker to indicate the length of strings. The only thing I cant it it to work for is finding the number of records because numberOfRecords = Lof(1) \ len(testRecord) is exactly what i did before to get the number of records, variable length strings are no use for numberOfRecords = Lof(1) \ len(testRecord), is there another way to retrive the number of records?
ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vaconDeclaringVariablesForRandomFileA Quote: > ccess.htm > > Would be nice if i could do something like; > > numberOfRecords = FileRecordCount(fileNum) 'Made up code (not > > real) > > while i have the file open > numberOfRecords = Lof(1) \ len(testRecord) > Armin
|
Fri, 22 Oct 2004 02:58:51 GMT |
|
 |
Larry Serflate #4 / 6
|
 number of records in a random access file?
Quote: > > Variable length strings in a structure are of no use for random files > > because random files have a fixed record length: > Well actually it seems to work you know. I can't understand how. Perhaps it > it using a marker to indicate the length of strings. The only thing I cant > it it to work for is finding the number of records because numberOfRecords = > Lof(1) \ len(testRecord) is exactly what i did before to get the number of > records, ...
As stated, that only works when each record is a specific length. Quote: > is there another way to retrive the number of records?
A. You could reserve the first 4 bytes of the file to hold an Integer that you keep up to date on the number of records in the file. B. You could create a second file as a list of Integers (4 bytes each) to index where in the data file each record is located. Deciding which to use would depend on how often would you need to, for example, find the Xth record in the file, where X is some 'random' number. Random Access, as its name implies, allows you to get any one record from anywhere in the file, without having to read through the file to get to the desired spot. If you don't need that capability, then A (above) would work with a minimum amount of effort. If you do need that capability, then you have to index the file, if you use your variable length strings. You will also have to be prepared to do periodic maintenance on the file. Without using fixed length strings, you can't (safely) allow the user to edit a record. You'd have to create a new record and mark the old one as invalid. Then periodically you would have to clean out all the invalid records... If editing is a requirement then you should read the section Armin linked to, (there is a full example in that area) to actually use fixed length records.... LFS
|
Fri, 22 Oct 2004 05:10:28 GMT |
|
 |
Lc3 #5 / 6
|
 number of records in a random access file?
Thankyou for your comments, i think you have helped, what i really want is to be able to use fixed length strings, but i cant, the example linked to used the code <VBFixedString(10)> or other length , which doesnt seem to work for me (they are just ignored and var len strings are made. Does it work for you?
Quote:
> > > Variable length strings in a structure are of no use for random files > > > because random files have a fixed record length: > > Well actually it seems to work you know. I can't understand how. Perhaps it > > it using a marker to indicate the length of strings. The only thing I cant > > it it to work for is finding the number of records because numberOfRecords = > > Lof(1) \ len(testRecord) is exactly what i did before to get the number of > > records, ... > As stated, that only works when each record is a specific length. > > is there another way to retrive the number of records? > A. You could reserve the first 4 bytes of the file to hold an Integer that > you keep up to date on the number of records in the file. > B. You could create a second file as a list of Integers (4 bytes each) > to index where in the data file each record is located. > Deciding which to use would depend on how often would you need > to, for example, find the Xth record in the file, where X is some 'random' > number. > Random Access, as its name implies, allows you to get any one record > from anywhere in the file, without having to read through the file to get to > the desired spot. If you don't need that capability, then A (above) would > work with a minimum amount of effort. If you do need that capability, > then you have to index the file, if you use your variable length strings. > You will also have to be prepared to do periodic maintenance on the > file. Without using fixed length strings, you can't (safely) allow the user > to edit a record. You'd have to create a new record and mark the old one > as invalid. Then periodically you would have to clean out all the invalid > records... If editing is a requirement then you should read the section > Armin linked to, (there is a full example in that area) to actually use fixed > length records.... > LFS
|
Fri, 12 Nov 2004 03:46:42 GMT |
|
 |
Karim Refeyto #6 / 6
|
 number of records in a random access file?
In fact, the attribute <VBFixedString(10)> allow you to write/read fixed length string to/from random access files. But if you check the length of your string when you put in "ABC", the length will not be 10 but 3. Because the language doesn't support fixed length strings. So, in conclusion : you can use the <VBFixedString(10)> attribute to write/read fixed length strings in files (in structures for example) but consider in your code than as variable length strings. Note : structures are not as fastest than in VB 6 to write/read in file because there are objects too. So forgot them... K.R. PS : The only way to have fixed length string in VB.NET is to use the VB6.FixedLengthString class but it's over complicated compared to the benefit (can't write/read in file, you must use Value property to access the string...) *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
|
Mon, 28 Mar 2005 01:16:37 GMT |
|
|
|