Write to new Binary File has junk characters at end of file

N

not aaron

I start out with a string. Which I then encode with my own algorithm
changing every characters ascii value depending on a key. I then save
it to a binary file.

When I generate the initial string, it shows up fine. When I encode
the string it shows up right. When I save the file it appends about 3
lines of random ascii characters (about the length of my original
string).

When I go back to decode the string, it works right for what it is
suppose to do, just those junk characters at the end of the file are
showing up as junk when I decode it. The application still works, it
just bothers me and looks strange.

I am using the following code to create the file:

////////
Dim ff As Integer = FreeFile()
FileOpen(ff, fileName, OpenMode.Binary, OpenAccess.ReadWrite)
FilePut(ff, encodedText)
FileClose(ff)
\\\\\\\\

Note: Before I write to the file, I msgbox the encodedText, and it
shows up right. After it's written, the file has junk at the end of
it.

How can I get the file to only produce what I want and nothing extra at
the end of it?

thanks
 
K

Ken Tucker [MVP]

Hi,

Why dont you use the binaryreader and binarywriter class instead.

http://msdn.microsoft.com/library/d...cpconreadingwritingtonewlycreateddatafile.asp

http://msdn.microsoft.com/library/d.../html/frlrfSystemIOBinaryWriterClassTopic.asp

http://msdn.microsoft.com/library/d.../html/frlrfsystemiobinaryreaderclasstopic.asp

Ken
--------------------
I start out with a string. Which I then encode with my own algorithm
changing every characters ascii value depending on a key. I then save
it to a binary file.

When I generate the initial string, it shows up fine. When I encode
the string it shows up right. When I save the file it appends about 3
lines of random ascii characters (about the length of my original
string).

When I go back to decode the string, it works right for what it is
suppose to do, just those junk characters at the end of the file are
showing up as junk when I decode it. The application still works, it
just bothers me and looks strange.

I am using the following code to create the file:

////////
Dim ff As Integer = FreeFile()
FileOpen(ff, fileName, OpenMode.Binary, OpenAccess.ReadWrite)
FilePut(ff, encodedText)
FileClose(ff)
\\\\\\\\

Note: Before I write to the file, I msgbox the encodedText, and it
shows up right. After it's written, the file has junk at the end of
it.

How can I get the file to only produce what I want and nothing extra at
the end of it?

thanks
 
N

not aaron

It seems that when I was creating the file, i was overwriting the one I
previously made. So when it overwrites the file, it adds the junk to
the end of it, and it is a new file, it won't. I won't complain, but
thanks for your quick response Ken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top