|
-
May 2nd, 2000, 10:05 PM
#1
Thread Starter
New Member
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
-
May 2nd, 2000, 10:12 PM
#2
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
-
May 2nd, 2000, 10:17 PM
#3
Hyperactive Member
Are you sure the length and termination string are not being stored? Isn't that how it works in Binary files?
-
May 2nd, 2000, 10:25 PM
#4
Hyperactive Member
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
-
May 2nd, 2000, 10:28 PM
#5
Frenzied Member
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.
-
May 3rd, 2000, 12:51 AM
#6
Thread Starter
New Member
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
-
May 3rd, 2000, 03:51 PM
#7
Hyperactive Member
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
-
May 3rd, 2000, 04:25 PM
#8
transcendental analytic
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.
-
May 3rd, 2000, 04:28 PM
#9
transcendental analytic
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.
-
May 4th, 2000, 03:32 AM
#10
Thread Starter
New Member
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.
-
May 4th, 2000, 03:43 AM
#11
Thread Starter
New Member
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
-
May 4th, 2000, 04:08 AM
#12
transcendental analytic
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.
-
May 4th, 2000, 07:15 AM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|