|
-
Aug 20th, 2001, 11:25 PM
#1
Thread Starter
New Member
problem in input string
hi,
i am reading a file & writing to other file but i found that output file have more characters then input file. my code is as follows, any one test with their file & give me some sugguestion.
Private Sub Command1_Click()
Dim tfile As String
Dim ofile As String
Dim outfile As String
Dim intf As Long
Dim intf1 As Long
intf = FreeFile
tfile = "c:\temp\iii.hex"
intf = FreeFile
Open tfile For Binary Access Read As #intf
outfile = Input(LOF(intf), #intf)
ofile = "c:\temp\177.hex"
intf1 = FreeFile
Open ofile For Output As #intf1
Write #intf1, 1, outfile;
Close #intf1
Close #intf
End
End Sub
thanx
arunpatel
-
Aug 20th, 2001, 11:55 PM
#2
Frenzied Member
I woulod think that if you open a file for binary access read you would want to read it into a byte array. Then open for binary access write.
PHP Code:
Function readBinFile(ByVal bfilename As String) As Variant
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
Dim binfilestr As String
On Error GoTo errHandler
FileNum = FreeFile
Open bfilename For Binary Access Read As #FileNum
fl = FileLen(bfilename)
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
readBinFile = binbyte
Exit Function
errHandler:
Exit Function
End Function
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Aug 21st, 2001, 12:47 AM
#3
Fanatic Member
The string method is fine. The only thing to watch for in this case is that if the file is pure binary data, you could trip up the Input() function. If by chance there's an ASCII 26 value in the file somewehre before the true end of the file, and you give Input() the file length to read, you'll cause an error. To get around it, either dim a string and give it the file size, or use a byte array.
VB Code:
'assuming you've dimmed outfile as string as you have, and have the file open
outfile = Space$(LOF(intf))
Get #intf, 1, outfile
Close #intf
'or you could do this
Dim outfile() As Byte
'assuming the same vars for the file and so on, and that you've opened it
ReDim outfile(0 To (LOF(intf) - 1))
Get #intf, 1, outfile
Close #intf
The extra chars are coming from Write most likely, which will insert extra ". You should only use Put/Get with binary mode files.
It looks like you're just copying the file too. You could just use FileCopy instead, unless there's a reason you wanted the data itself that you just didn't mention here.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 21st, 2001, 12:56 AM
#4
The extra characters are coming from this line:
Write #intf1, 1, outfile;
I'm not sure what you're trying to do, but that line puts this into the new file:
1,"Contents",
..where Contents is the contents of the outfile string.
If you're trying to just write out the contents of the string and nothing else, try this line instead:
Print #intf1, outfile;
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
|