REQ: Declaring variables and file for random access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefanie

    REQ: Declaring variables and file for random access

    Hi,

    I have a question about declaring variables for writing data to a file for
    random access.
    I have two string variables declared in a module (user-defined types).
    The point is that for one of them i can't define (hard coded) a fixed
    length because the application is reading data
    from another file (In that file they have the same lenght).
    Is it possible to use the lenght i obtain from reading that other file to
    declare my variable or are there
    other possibilities.

    Thanks in advance,

    Stefanie


  • Martin Trump

    #2
    Re: REQ: Declaring variables and file for random access

    In message <157fd$41b32ced $513b4d44$1464@ news1.zonnet.nl >, Stefanie
    <Stefanie@zsmai l.net> writes[color=blue]
    >file they have the same lenght). Is it possible to use the lenght i obtain from
    >reading that other file to
    >declare my variable or are there
    >other possibilities.[/color]

    Off the top of my head. You can define a dynamic array within a user
    defined type:-

    Type mytype
    ....
    mystring()
    ...
    End Type

    You can then set the size of mytype.mystring using:-

    Redim mytype.mystring (Len(inputstrin g))

    However, you may be storing up trouble if you want to use random access
    on the resulting file. Where do you seek to for a new record?
    HTH.

    Regards

    --
    Martin Trump

    Comment

    • Steve Gerrard

      #3
      Re: Declaring variables and file for random access


      "Stefanie" <Stefanie@zsmai l.net> wrote in message
      news:157fd$41b3 2ced$513b4d44$1 464@news1.zonne t.nl...
      | Hi,
      |
      | I have a question about declaring variables for writing data to a file
      for
      | random access.
      | I have two string variables declared in a module (user-defined types).
      | The point is that for one of them i can't define (hard coded) a fixed
      | length because the application is reading data
      | from another file (In that file they have the same lenght).
      | Is it possible to use the lenght i obtain from reading that other file
      to
      | declare my variable or are there
      | other possibilities.
      |
      | Thanks in advance,
      |
      | Stefanie
      |

      I think what you are after is how to write a string to an output file
      with a specified field width, without using a fixed length string, since
      you don't know the fixed length needed until run time.

      If so, then you should be able to do something like this:
      n = FixedLengthNeed ed
      j = Len(StringToWri te)

      k = n - j ' spaces needed

      If k > 0 Then
      ' print string and pad with spaces
      Print #FileNum, StringToWrite, Space$(k)
      Else
      ' clip string to maximum length
      Print #FileNum, Left$(StringToW rite, n)
      End If


      Comment

      • Stefanie

        #4
        Re: Declaring variables and file for random access

        Steve,

        Thats the same as writing data to the file using Put, right ?

        Stefanie


        Comment

        • J French

          #5
          Re: Declaring variables and file for random access

          On Mon, 6 Dec 2004 11:49:27 +0100, "Stefanie" <Stefanie@zsmai l.net>
          wrote:
          [color=blue]
          >Steve,
          >
          >Thats the same as writing data to the file using Put, right ?
          >[/color]

          I could not see your first post

          Below is a demo of Random Access using UDTs containing variable length
          strings - the Record Length that it is opened with has to be large
          enough.
          - I would not do it like this personally

          Option Explicit

          Private Type TRec
          L As Long
          S As String
          I As Integer
          End Type

          Private Sub Command1_Click( )
          Dim Rec As TRec, Channel%

          Rec.L = -1
          Rec.S = "Test String"
          Rec.I = -1

          Channel = FreeFile
          Open App.Path + "\test.dat" For Random As Channel Len = 200
          Put #Channel, 1, Rec
          Close #Channel

          End Sub

          Private Sub Command2_Click( )
          Dim Rec As TRec, Channel%

          Channel = FreeFile
          Open App.Path + "\test.dat" For Random As Channel Len = 200
          Get #Channel, 1, Rec
          Close #Channel
          Me.Print Rec.L, Rec.S, Rec.I

          End Sub

          Comment

          • Steve Gerrard

            #6
            Re: Declaring variables and file for random access


            "Stefanie" <Stefanie@zsmai l.net> wrote in message
            news:38251$41b4 3937$513b4d44$2 6882@news1.zonn et.nl...
            | Steve,
            |
            | Thats the same as writing data to the file using Put, right ?
            |
            | Stefanie
            |

            I'm hoping you have access to help files and can look up Put. Its
            behavior with variable length strings varies according to whether they
            are in a UDT, and whether the file is opened Random or Binary.

            As far as I can tell, Put will write a 2 byte length before writing a
            variable length string to a file opened Random (UDT or not), which
            allows Get to retrieve it correctly. The total record length would have
            to be long enough for that. I don't believe you actually have to pad out
            the string length unless you want to.



            Comment

            • Randy Birch

              #7
              Re: Declaring variables and file for random access

              Interesting! I tried something similar yesterday, and got a 'bad record
              length error when trying to do the Put.

              --


              Randy Birch
              MS MVP Visual Basic



              "J French" <erewhon@nowher e.uk> wrote in message
              news:41b469f6.8 8497051@news.bt click.com...
              : On Mon, 6 Dec 2004 11:49:27 +0100, "Stefanie" <Stefanie@zsmai l.net>
              : wrote:
              :
              : >Steve,
              : >
              : >Thats the same as writing data to the file using Put, right ?
              : >
              :
              : I could not see your first post
              :
              : Below is a demo of Random Access using UDTs containing variable length
              : strings - the Record Length that it is opened with has to be large
              : enough.
              : - I would not do it like this personally
              :
              : Option Explicit
              :
              : Private Type TRec
              : L As Long
              : S As String
              : I As Integer
              : End Type
              :
              : Private Sub Command1_Click( )
              : Dim Rec As TRec, Channel%
              :
              : Rec.L = -1
              : Rec.S = "Test String"
              : Rec.I = -1
              :
              : Channel = FreeFile
              : Open App.Path + "\test.dat" For Random As Channel Len = 200
              : Put #Channel, 1, Rec
              : Close #Channel
              :
              : End Sub
              :
              : Private Sub Command2_Click( )
              : Dim Rec As TRec, Channel%
              :
              : Channel = FreeFile
              : Open App.Path + "\test.dat" For Random As Channel Len = 200
              : Get #Channel, 1, Rec
              : Close #Channel
              : Me.Print Rec.L, Rec.S, Rec.I
              :
              : End Sub
              :

              Comment

              • J French

                #8
                Re: Declaring variables and file for random access

                On Mon, 6 Dec 2004 22:36:52 -0500, "Randy Birch"
                <rgb_removethis @mvps.org> wrote:
                [color=blue]
                >Interesting! I tried something similar yesterday, and got a 'bad record
                >length error when trying to do the Put.[/color]

                Yes, it will fail if the data is wider than the 'record length'
                - something that needs to be manually checked

                Personally I would be /very/ wary of using it

                IMO data written to disk should be 'language independent'


                Comment

                • Stefanie

                  #9
                  Re: Declaring variables and file for random access

                  Steve,
                  [color=blue]
                  > As far as I can tell, Put will write a 2 byte length before writing a
                  > variable length string to a file opened Random (UDT or not), which
                  > allows Get to retrieve it correctly. The total record length would have
                  > to be long enough for that. I don't believe you actually have to pad out
                  > the string length unless you want to.[/color]

                  Yes i read that too, but i don't understand how to do this:

                  This are the variables declared in my module.

                  Type Record
                  vDateTime As String * 19
                  vFileName As String
                  End Type

                  Public RecVar As Record

                  Using vFileName As String * 255 gives my enough for een full path, but most
                  of the time it's wasting space.

                  I use this for opening my file:

                  Open strFileName For Random As #Dnr Len = Len(RecVar)

                  But it's gives an error. "Bad record lenght"

                  Maybe somebody can offer me an example how to use the aforesaid.

                  Stefanie


                  Comment

                  • J French

                    #10
                    Re: Declaring variables and file for random access

                    On Thu, 9 Dec 2004 12:38:00 +0100, "Stefanie" <Stefanie@zsmai l.net>
                    wrote:
                    [color=blue]
                    >Steve,
                    >[color=green]
                    >> As far as I can tell, Put will write a 2 byte length before writing a
                    >> variable length string to a file opened Random (UDT or not), which
                    >> allows Get to retrieve it correctly. The total record length would have
                    >> to be long enough for that. I don't believe you actually have to pad out
                    >> the string length unless you want to.[/color]
                    >
                    >Yes i read that too, but i don't understand how to do this:
                    >
                    >This are the variables declared in my module.
                    >
                    > Type Record
                    > vDateTime As String * 19
                    > vFileName As String
                    > End Type
                    >
                    > Public RecVar As Record
                    >
                    >Using vFileName As String * 255 gives my enough for een full path, but most
                    >of the time it's wasting space.
                    >
                    >I use this for opening my file:
                    >
                    > Open strFileName For Random As #Dnr Len = Len(RecVar)
                    >
                    >But it's gives an error. "Bad record lenght"
                    >
                    >Maybe somebody can offer me an example how to use the aforesaid.[/color]

                    Look at the example I posted earlier in this thread

                    If you are interested in not wasting space then look carefully at
                    opening the file in Binary mode rather than Random mode

                    Have a careful look at the Seek Statement, and more importantly the
                    Seek Function.

                    Comment

                    • Steve Gerrard

                      #11
                      Re: Declaring variables and file for random access


                      "Stefanie" <Stefanie@zsmai l.net> wrote in message
                      news:37e66$41b8 391c$513b4d44$1 0299@news1.zonn et.nl...
                      | Steve,
                      |
                      | > As far as I can tell, Put will write a 2 byte length before writing
                      a
                      | > variable length string to a file opened Random (UDT or not), which
                      | > allows Get to retrieve it correctly. The total record length would
                      have
                      | > to be long enough for that. I don't believe you actually have to pad
                      out
                      | > the string length unless you want to.
                      |
                      | Yes i read that too, but i don't understand how to do this:
                      |
                      | This are the variables declared in my module.
                      |
                      | Type Record
                      | vDateTime As String * 19
                      | vFileName As String
                      | End Type
                      |
                      | Public RecVar As Record
                      |
                      | Using vFileName As String * 255 gives my enough for een full path, but
                      most
                      | of the time it's wasting space.
                      |
                      | I use this for opening my file:
                      |
                      | Open strFileName For Random As #Dnr Len = Len(RecVar)
                      |
                      | But it's gives an error. "Bad record lenght"
                      |
                      | Maybe somebody can offer me an example how to use the aforesaid.
                      |
                      | Stefanie
                      |

                      Len(RecVar) will not include any space for the vFileName string if it is
                      declared as variable length, hence the bad record length error. You
                      would have to make the Len in the Open statement equal to the longest
                      length you expect: approx. Len(RecVar)+255 .

                      If you don't want to reserve 255 for every vFileName, you can't really
                      use a fixed record length, which means working in Binary rather than
                      Random. It gets a little more complicated, but Jerry French can help you
                      out if you need it. Me, I would just waste some disk space, it seems
                      cheap enough, and go with a fixed length record. 512 sounds like a nice
                      size.



                      Comment

                      Working...