Results 1 to 8 of 8

Thread: [RESOLVED] Random acces files. What am I doing wrong

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,086

    Resolved [RESOLVED] Random acces files. What am I doing wrong

    This code works in one app I have written but not in another. Where am I going wrong? I know the answer will be simple

    In Dims

    Code:
    Private FBU2 As IsolateFileNo
    
    'Type
    
    Private Type IsolateFileNo
        Fill As String * 3
        OurFileNumber As String * 7
        FillMore As String * 245
    End Type 'records are 255 long for other data to be resolved later
    Later, after form load and with variables specified...

    Code:
     Dim aRecord As Integer
        Dim Mike As String
        Open FilePath & FirstFile For Random Access Read As #1 Len = 255
        'Open FilePath & SecondFile For Random As #2 Len = 255
        'Open FilePath & ThirdFile For Random As #3 Len = 255
        Open FilePath & FourthFile For Random Access Write As #4 Len = 255
        'KeyPreview = True
        y = 0 'initialise fourth file record number
            For aRecord = FirstFileFirstRecord To FirstFileLastRecord
            Debug.Print aRecord; 'works to here
            Get #1, aRecord, FBU2
            Mike = FBU2.ClaimNo ' this is where it fails! 
            Debug.Print " and " & Mike;
            y = y + 1 'increment fourth file record number
            Put #4, y, FBU2
       Next
    Last edited by el84; Oct 6th, 2022 at 12:54 AM.
    Thanks all!

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,429

    Re: Random acces files. What am I doing wrong

    You are using the hardcoded 255.
    That's not correct, a string character occupies 2 bytes.
    Code:
    Option Explicit
    
    'Type
    Private Type IsolateFileNo
        Fill As String * 3
        OurFileNumber As String * 7
        FillMore As String * 245
    End Type 'records are 255 long for other data to be resolved later
    
    Private FBU2 As IsolateFileNo
    
    
    Private Sub Form_Load()
      ' The following prints 510
      Debug.Print LenB(FBU2)
    End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,086

    Re: Random acces files. What am I doing wrong

    Quote Originally Posted by Arnoutdv View Post
    You are using the hardcoded 255.
    That's not correct, a string character occupies 2 bytes.
    Code:
    Option Explicit
    
    'Type
    Private Type IsolateFileNo
        Fill As String * 3
        OurFileNumber As String * 7
        FillMore As String * 245
    End Type 'records are 255 long for other data to be resolved later
    
    Private FBU2 As IsolateFileNo
    
    
    Private Sub Form_Load()
      ' The following prints 510
      Debug.Print LenB(FBU2)
    End Sub
    I don't think that is the answer, I'm afraid...
    That code works perfectly in another application.
    A string of letters say 10 characters long cannot it into 2 bytes on a text file on a disk, surely? Because each character must take up a byte.
    Thanks all!

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,803

    Re: Random acces files. What am I doing wrong

    As with so many other uses of UDTs, this is a case where any String data gets converted from internal Unicode to external ANSI. The ancient fixed-String type is written without a leading length header and padded out with spaces to the specified length.

    So this:

    Code:
    Option Explicit
    
    Private Type SomeRec
        S As String * 10
    End Type
    
    Private Sub Main()
        Dim SomeRec As SomeRec
        Dim F As Integer
    
        SomeRec.S = "abcdefg"
        F = FreeFile(0)
        Open "some.dat" For Random Access Write As #F Len = Len(SomeRec)
        Put #F, 1, SomeRec
        Close #F
    End Sub
    ... will create a 10 byte ANSI file.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,425

    Re: Random acces files. What am I doing wrong

    Quote Originally Posted by el84 View Post
    Because each character must take up a byte.
    I didn't study it in detail, but a character takes two bytes while in VB6's memory. However, once written to disk (through normal VB6 disk I/O operations), it only takes one byte on disk.

    VB6 does this ANSI-to-Unicode (and vice-versa) conversion when reading/writing strings from/to disk. So, your record sizes only need to reflect one-byte-per-character.

    A Len(VarDeclaredWith_IsolateFileNo) also works for your record size, and you don't have to figure it out.
    Last edited by Elroy; Oct 6th, 2022 at 11:13 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,537

    Re: Random acces files. What am I doing wrong

    Mike = FBU2.ClaimNo ' this is where it fails!
    I don't see a field called ClaimNo in the UDT.
    Code:
    Private Type IsolateFileNo
        Fill As String * 3
        OurFileNumber As String * 7
        FillMore As String * 245
    End Type 'records are 255 long for other data to be resolved later
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,086

    Re: Random acces files. What am I doing wrong

    Thanks Passel. That is one error. I've fixed that but still no go, I'm afraid.

    Re lengths of strings/bytes etc. discussed earlier, the reason for {name} As String * n in a UDT is that space is reserved for a whole string of characters, not one. That part in my code is correct (eg a ten char string takes up 10 bytes in a random disk file). It is used in numerous apps I have written. I have made some other silly little error this time (like the one Passel pointed out).
    Thanks all!

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,086

    Re: Random acces files. What am I doing wrong

    Update to all interested. Stupid small mistake, as I suspected.

    There was nothing wrong with the logic (or the UDT, including lengths of strings). The error was caused by my filename for 'FirstFile', which I had not included in the posted code. It happened to be a simple typo rendering the file non-existent, and of course everyone will have assumed that I had checked that. I hadn't (until today) as I was so intent on the logic.

    I'll mark the thread resolved... (and I will re-resolve to check for minor errors like typos!)
    Thanks all!

Posting Permissions

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



Click Here to Expand Forum to Full Width