Results 1 to 13 of 13

Thread: Put to a Binary File does not do as I expected

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 1999
    Location
    Ottsville, PA, USA
    Posts
    9
    The following code Puts the string in the correct
    location (usually), but extra garbage is also attached
    leading up to my string. The extra garbage is usually
    control characters, but sometimes it's bits and piece of
    recognizable text. It look like a classic "pointer gone
    astray" type thing.

    Private Sub Command1_Click()
    Dim Mystring As String
    Mystring = "abcdefg"
    filenum = FreeFile
    Open "c:\test.txt" For Binary As filenum
    Put #filenum, 10, Mystring
    Close filenum
    End Sub

    I'm expecting test.txt to be:
    abcdefg
    but I'm getting:
    @&^~)- 47 abcdefg

    I am looking at the file with NotePad after I have created
    it, on 95, VB4.0

    Thanks very much.
    Phil Hall

  2. #2
    Guest
    I've had the same problem before and it's usually due to a variable which has not been declared explicitly as a string (sort of a variant gone astray).
    Any chance of that?

    -John

  3. #3
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Question

    Are you sure the length and termination string are not being stored? Isn't that how it works in Binary files?

  4. #4
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Talking I just checked...

    Phil,

    You are Put-ting more data than you want to, that's where the garbage comes from. Try

    Code:
    Put #filenum,,MyString

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Binary Mode for Strings?

    I believe that Binary Mode Files write exact copies of what is in memory and read back the same way. A String has control data with it. I think that is what you are seeing.

    The strings I write to a file are usually declared as fixed length (EG: Dim Abc As String*10). Then Abc appears in memory and in the file as ten characters, no more, no less.

    There are other ways of dealing with files, which I do not use. Perhaps these are useful for variable lenght strings.
    there are "TextStream" Files written to using Write statements.

    Check VB documentation
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 1999
    Location
    Ottsville, PA, USA
    Posts
    9
    Judd--

    the code:
    Put #filenam,,Mystring

    puts data at the current location in the file. I wanted to
    move around in the file and drop things wherever. I could
    set the current cursor with Seek and the do the Put at the
    current location. I could and will probably end up using the Print command, but I wanted to understand what was going on with Put.

    It does seem that Put is Put'ing extra string defining
    characters with it. I'll try fix lengthed strings to see
    if that helps, but it is somewhat limiting. I'm also
    writing data to this file.

    I'm used to doing C file I/O, where you can just go into
    any spot in the file, write 1 character, and get out.

    Thanks everybody for the help.

    Phil Hall

  7. #7
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Arrow

    Phil,

    I'm messed around with 'Put' a little...and I couldn't replicate your problem. Its seems to work fine for VB6 SP3

    That's not much help I know....but did you try a few different negths of string and positions?

    I did a few with a loop on the position and it all seemed fine...



    Dan

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It seems youre complaining about nothing. Put works just like it should work. About your first post: the things before the text is the characters that you didn't overwrite. Put just putted the string starting at the tenth byte.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Oh, You don't need the seek statement. Just use
    Put#yourfilenumber, Position in file, String
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 1999
    Location
    Ottsville, PA, USA
    Posts
    9
    Judd,
    It's good to know about it working in VB6. I'll see
    if I can try it in 6. I haven't yet tried fixed
    lenght strings yet. I will tonight.

    Kedaman,
    In general I don't use Seek, I use:
    Put #filenum,position,"text"

    And, no, it's not just writing into the file starting at
    position. Characters from the beginning of the
    file up to "position" are over written--not all of
    them, but a lot, and it's different each time.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 1999
    Location
    Ottsville, PA, USA
    Posts
    9
    Kedaman,

    I think you might be right. I am creating/overwriting
    a new file each time. And for some reason, I assumed
    that all the characters up to my write position would be
    blank. And any text that I stored in the beginning of
    the file was getting written over because the whole
    file was getting written over.

    I'll go try it again with append, but I'm sure that's
    it. Thanks.

    Phil Hall

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I have used Put, my whole life so i know how it works
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 1999
    Location
    Ottsville, PA, USA
    Posts
    9
    Yes indeed Kedaman, you are right.

    Put works exactly as it's supposed to. The thing I didn't
    realize is that upon opening a brand new file, and put'ting
    to a position other than the 1st position, you will have
    garbage from position 1 to the position you Put to.

    Earlier I made the statement that an existing file was
    being over written. That was incorrect. The thing to
    remember is one has to start writing something at position
    1 when writing to a new file. In my app, I'll have to write
    a lot of spaces up to my desired text start.

    Thanks everyone for the help.

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