Results 1 to 14 of 14
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordXP)

    Chris

    There appears to be a missing reference to s.dot, used by Call slave.WriteToFileName(strFile, strar)
    I think removal of the "slave." will work, since you've duplicated the function in a.dot.

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: variant arrays on file lack descriptors (WordXP)

    In the first place, this is not specific to Word. Get and Put are general VBA instructions, originating in old versions of BASIC.

    In the second place, the online help states explicitly that "For any array other than an array in a user-defined type, Put writes only the data. No descriptor is written." if the mode is Binary. If you open the file in Random mode, the descriptor IS written. If you replace Binary with Random, you can omit the ReDim statement in MyAppRead - Get will read the descriptor written by Put.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    variant arrays on file lack descriptors (WordXP)

    Boy, is my face red. As Hans has pointed out, I have confused Random with Binary mode. I made the change he suggested and things appear to be working as they should.

    <font color=ff8c00>Every data item within Word/VBA has a descriptor, Word/VBA wouldn't be able to do things with the data otherwise. In particular variant arrays have descriptors. But when variant arrays are written to disk using PUT, the descriptor is dropped and the variant array is written as a series of elements of the array, as the snapshot shows.
    If you want to obtain the complete array, you must remember how many elements were written. Clearly this is not going to be possible if the file containing a data array has come from a 3rd party. I believe that this is a failing on Word's part - data should be switchable between RAM memory and the hard drive. That they choose to implement it partway is proof that they considered it doable. That they stopped part way is at the very least a shame.

    The attached ZIP file holds an application that you can run to duplicate the behaviour. The macro MyAppWrite quite happily writes five elements to disk. The macro MyAppRead will read back those elements and correctly load them to the array only if you pre-dimension the array. This dimension must be communicated to the macro. It will not be available to you from the file (without a personal binary examination of the file to rebuild the descriptor that Word/VBA should have built in the first place).

    Various errors can arise, several reported as bugs in the VB6 engine. I included a couple of the MSKB numbers. below is a hex view of the file; I took the liberty of underlining in Red the string lengths. The file is written as a series of elements with no indication of the number of elements of the variant array.</font color=ff8c00>

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    Alan, thanks. In my haste to post the "problem", I forgot to remove the references aftyer I'd pasted the procedures in from my slave library.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    Hans, thanks for putting me right. I'd be contenty if this entire thread were deleted!

    I posted as "Word/XP" because that's where i was experimenting, and yes, I was confused after reading the help files. I've since switched to random and things appear to be working as they should.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: variant arrays on file lack descriptors (WordX

    Actually, I'm grateful - you inspired me to look into a subject I don't know much about. I learned something new from it.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    Well, this is your lucky day:
    As-is, the little TEST macro runs flawlessly on my Word/XP system.
    When I enable the strBr(3) assignment I get a length error on the PUT.
    When I enable the OPEN with LEN, and disable its cousin, the length error goes away.

    Our trivial examples with variant array elements set to "zero", "one" etc. seem to rely on a default LENgth of, say, 128 for the buffer. As soon as I try to store a variant array of 6,000+ file names (with paths), I get the length error.
    So far it seems to suggest that I need to calculate a buffer length based on the sum of the lengths of each element of my array, that is, I feel myself being forced back into the corner of dealing with the descriptor,
    I'm trying not to sound more petulant than I seem. I'm struggling to understand why this descriptor-business seems to go off half-cocked.

    Does it seem to you that it would be nicer if Word/VBA would actually cope with the variant array? I know that it doesn't; I know that to get this working I'm probably going to abandon my idea of obtaining an array of file names and dropping it onto disk. I already have code that dumps an array count as the first record, and string elements as succeeding records. I can resurrect that.

    The help files for PUT tell me "If the length of the data being written is greater than the length specified in the Len clause of the Open statement, an error occurs."
    The help files for OPEN tell me "reclength Optional. Number less than or equal to 32,767 (bytes). "
    Together this tells me that variant arrays written to disk aren't going to be useful to me in general, when I'm working with massive numbers of files.

    Why the array? I search the drive for files, and that takes time; once I've established the file set, I want to re-run tests on the defined set (a contract with the client) without re-examining the hard drive.

    <pre>Public Function WriteArrayFromFileNumber(intFile As Integer, strAr())
    'Bad record length (Error 59)
    Put #intFile, , strAr
    End Function
    Sub TESTWriteArrayFromFilename()
    Dim strBr()
    ReDim strBr(3)
    Dim lng As Long
    lng = 30
    strBr(0) = String$(lng, "z")
    strBr(1) = String$(lng, "o")
    strBr(2) = String$(lng, "t")
    ' strBr(3) = String$(lng, "h")
    Dim intFile As Integer
    intFile = FreeFile
    ' Open "c:temp1.txt" For Random As intFile Len = 2000
    Open "c:temp1.txt" For Random As intFile
    Call WriteArrayFromFileNumber(intFile, strBr)
    Close intFile
    End Sub
    </pre>


  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: variant arrays on file lack descriptors (WordX

    You're using a file with just one record, but if the file has many records, Basic has to know how long those records are going to be.

    If you're dealing with thousands of file names, dumping them all in one record isn't going to work, as you mention yourself.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    If I understand you, you understand me (grin!), that I wanted a file with one record. That record would be, to me, a variant array. That my one record represents thousands of useful things, wouldn't alter it being, in all, a single record, albeit a large one, my variant array is a single item, an array.

    The limit of 32K on record size stymies my efforts in any case, and it's that that will limit me and force me back to one-record-per-element processing.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    At the risk of flogging the proverbial, and more for the sake of public record, here is yet another part of my confusion. This morning I had reason to re-visit code that deals with Most Recently Used (MRU) stuff. I have tried this over the past couple of years with regular INI files, INI files that deal with duplicate keys in a section, the registry's VB and VBA Program Settings entries. I've also played with storing custom records in my own data files. An example appears attached. This morning, to my surprise, I found that I was writing a single item to a file in Binary, not Random mode, and it appears to be working. Obviously I have read the help files for GET that say in part "On disk, a dynamic array in a user-defined type (written with Put) ...".

    This code (attached) appears to have been working for a month or two. And yet, discussion of the past two days leads me to believe that these Write/Read functions of mine have been effective ONLY because the data block is less than 32K, which will usually be the case for an application that concerns itself with MRU. That is, to date I've got away with it; however, once I want to set Write/read as generalised library functions for transmitting arrays to/from disk, I can place no guarantee on the size of the developer's array.

    In consequence it seems to me that any generalised developer function that writes/reads arrays, of any type, to disk must be prepared to deal with some sort of descriptor. For the most likely case - a vector of string elements, such as my 6,000+ list of files, or my MRU list, a simple file structure where the first record holds the count of entries, and the remainder are the elements of the array, will suffice.

    Again, this is more for the record. I'm happy to enter into debate, but at this point I'm still moving towards writing my own generalised write/read array routines.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: variant arrays on file lack descriptors (WordX

    Since you open the file in Binary mode, no array descriptor gets written to disk, so you have to ReDim the mru array explicitly in ReadArrayFromFile - and you do! (Item descriptors do get written to disk, but not a descriptor for the array dimensions). And again, yes, if you want your routines to be able to handle data blocks over 32K in size, you won't be able to read/write them in one Get/Put instruction.

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    Chris

    I've been following the discussion here, but I'm not quite sure what you're "getting at". I can see that the Put/ Get functions didn't perform as you had expected/ hoped with file I/O in Binary mode. But, as revealed, there are alternatives. If it's this that you're not happy with, you might be interested in (arguing with?) my take on it.

    As you say, the files/ recordsets you're dealing with might be from or destined to unknown sources. The put & get methods for binary files have been around for a long time, are present in many programming languages and are fairly generic. I'd therefore advocate that MS "retained" this fairly common and simple method of I/O access to such ends. As discovered, there are VB-specific alternative methods provided as well. Point being I guess, that passing such files around should require the programmer to be able pull them apart simply and then use them to his/her own ends, rather than have to deal with a whole lot of VB-specific header info etc. This might include keeping a count of the number of strings pulled up to EOF. So rather than having a heap of language-specific string (array) functions, the person at the other end could use the same Put & Get functions, regardless of the (unknown) source.

    I know this doesn't actually come off in practice - for instance, a C-string uses a null terminator and can't contain one as a character, while a VB string can - so it's not cut & dried. But I do think that in the case of distributed "generic" files, the onus is on the programmer to get & put the information from/ to the simple binary format, as devoid of as many "descriptors" as possible.

    <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15>
    Alan

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    > .. you ... ReDim the mru array explicitly ...

    Oh eagle-eyed Hans! yep. That was during my interim phase of "mebbe I can do this with a little help from an INI file", a phase I soon left in R&D because it made the recipient dependant on external knowledge of the array, and I wanted the incoming file to hold all the information necessary. Obviously I spawned off a copy during my time with MRUs, where it fossilized into a red-herring. Appealing, no doubt, to an eagle ....

    VBA's built-in array handling is going to be useful only with 32K or less, and that suggests limits on internal (developer-owned) data, but not on external (user-owned) data. For external data I'm going to have to rely on some form of developer-defined data structure.

    Thanks for nudging my thinking back into line.

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: variant arrays on file lack descriptors (WordX

    Alan, thanks for chiming in.

    > I'm not quite sure what you're "getting at".
    I think you are, and your comments illustrate this. I'm operating (this year!) in a VBA environment, writing aps that are designed to run in VBA, so I saw communication of arrays to files as a VBA thing, and wanted VBA, which already has a descriptor for everything I play with, to use that descriptor data and write it to disk.

    You are correct in pointing out that there is a world outside of VBA, and quite rightly, it makes sense for the programmer to deal with this. In terms of Binary files this would mean placing a programmer-descriptor at the head of the file. Such a method I would use were I communicating with APL, or mainframe COBOL, etc.

    Locked as I was in my little VBA world, I thought it made more sense for VBA to include the descriptor on file. My reasoning on "It ought to be possible" is, I think, valid. If VBA knows enough about something to let me assign it in RAM memory, then it knows enough about it to let me assign it to a disk. However, that doesn't mean that the assignment to disk will be universally useful - it will be useful specifically to other VBA applications, that would be the only guarantee, so while in principle it's possible (and hasn't been implemented), in practice it doesn't really make sense to other users of disk (APL, COBOL, ...), and so in practice it hasn't been done.

    This too explains why we don't get to write Objects and the like, in general, to file , as objects, but must pull them apart and write them in a more universal format designed by programmers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •