dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: how can i open 2 file?

Hybrid View

  1. #1
    Join Date
    Jul 2007
    Posts
    12

    how can i open 2 file?

    let's say i have a .dat file and .hdr file .... i have to compare both of them .... how can i open both of them at the same time and then compare them ? do you think is it prossible? pls help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how can i open 2 file?

    Are they the same format? You could check the size, which would be quick, but if it's binary on one file, and text on the other, you can't.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: how can i open 2 file?

    You can even use Windows Internal command fc. You should use Shell to execute this command.

    The only condition here is that the files should be text files.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how can i open 2 file?

    This is a quick and dirty FileCompare. It works with text and binary files as well.
    Just call it with two valid filenames.
    It reads both files consecutively into strings and compares them afterwards.
    Code:
    Private Function FileComp(File1 As String, File2 As String) As Boolean
      Dim s1$, s2$, f%
      f = FreeFile
      Open File1 For Binary As #f: s1 = Space$(LOF(f)): Get #f, , s1: Close f
      Open File2 For Binary As #f: s2 = Space$(LOF(f)): Get #f, , s2: Close f
      FreeFile f
      FileComp = s1 = s2
    End Function
    If you want to do some fancy comparing and keep both files open, the code would look like:
    Code:
    Private Function FileComp2(File1 As String, File2 As String) As Boolean
      Dim f1%, f2%
      f1 = FreeFile: Open File1 For Binary As #f1
      f2 = FreeFile: Open File2 For Binary As #f2
      'now both files are open to do your reading and comparing
      Close #f2: FreeFile f2
      Close #f1: FreeFile f1
    End Function

  5. #5
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: how can i open 2 file?

    From an old project, fast and efficient, note that you may want to change the buffer size.

    Note that this function just tell if the two files are differents. If you want to compare the datas and tells the difference or something, this code would be a good frame to start on:

    Code:
    Private Function filesAreDuplicate(strFile1 As String, strFile2 As String)
    
        ' Open both file and compare them
        Const BUFFERSIZE = 10240
        Dim file1 As Integer
        Dim file2 As Integer
        Dim lngRemaining1 As Long
        Dim lngRemaining2 As Long
        Dim lngRemaining As Long
        Dim bolFilesAreDuplicate As Boolean
        Dim bolFinish As Boolean
        Dim buffer1 As String
        Dim buffer2 As String
        
        buffer1 = Space$(BUFFERSIZE)
        buffer2 = Space$(BUFFERSIZE)
        
        file1 = FreeFile
        Open strFile1 For Binary Access Read As #file1
        
        file2 = FreeFile
        Open strFile2 For Binary Access Read As #file2
        
        lngRemaining1 = LOF(file1)
        lngRemaining2 = LOF(file2)
        
        If lngRemaining1 <> lngRemaining2 Then
            bolFilesAreDuplicate = False
        Else
            bolFilesAreDuplicate = True
        End If
        
        lngRemaining = lngRemaining1
        
        While bolFinish = False And bolFilesAreDuplicate = True And _
            EOF(file1) = False And EOF(file2) = False
            
            If lngRemaining < BUFFERSIZE Then
                buffer1 = Space$(lngRemaining)
                buffer2 = Space$(lngRemaining)
            End If
        
            Get #file1, , buffer1
            Get #file2, , buffer2
            DoEvents
            
            If buffer1 <> buffer2 Then
                bolFilesAreDuplicate = False
            End If
            
            lngRemaining = lngRemaining - Len(buffer1)
            
            If lngRemaining = 0 Then
                bolFinish = True
            End If
        Wend
     
        filesAreDuplicate = bolFilesAreDuplicate
        
        Close #file2
        Close #file1
        
    End Function
    JeffB
    Last edited by JeffB; July 21st, 2007 at 10:02 AM.
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  6. #6
    Join Date
    Jul 2007
    Posts
    12

    Re: how can i open 2 file?

    thank you... really appreciate it.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)