Results 1 to 4 of 4

Thread: problem in input string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    baroda, india
    Posts
    12

    Exclamation 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
    arun

  2. #2
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    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.

  3. #3
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    1. 'assuming you've dimmed outfile as string as you have, and have the file open
    2. outfile = Space$(LOF(intf))
    3. Get #intf, 1, outfile
    4. Close #intf
    5.  
    6. 'or you could do this
    7. Dim outfile() As Byte
    8. 'assuming the same vars for the file and so on, and that you've opened it
    9. ReDim outfile(0 To (LOF(intf) - 1))
    10. Get #intf, 1, outfile
    11. 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

  4. #4
    Tygur
    Guest
    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
  •  



Click Here to Expand Forum to Full Width