-
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
-
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
-
Are you sure the length and termination string are not being stored? Isn't that how it works in Binary files?
-
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
-
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
-
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
-
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...
:cool:
Dan
-
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.
-
Oh, You don't need the seek statement. Just use
Put#yourfilenumber, Position in file, String
-
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.
-
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
-
I have used Put, my whole life so i know how it works :)
-
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.