Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

File, No Edit, View

Hadji_13Hadji_13 Member Posts: 274
Hi

I want to compaire two files,

I've been using this code:

Hi,

This SHOULD work right. Right?

Open "c:file.dat" for input as #1
Open "c:file1.dat" for input as #2

Do until eof(1)

Input #1, t
input #2, u

if t <> u then
'These files are differant
end if

loop

But it doesn't totally work. Do I need to use binary mode or something? How?

Cheers

Raz
----------------------------------------------------
The solution to the problem changes the problem

Comments

  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : Hi
    :
    : I want to compaire two files,
    :
    : I've been using this code:
    :
    : Hi,
    :
    : This SHOULD work right. Right?
    :
    : Open "c:file.dat" for input as #1
    : Open "c:file1.dat" for input as #2
    :
    : Do until eof(1)
    :
    : Input #1, t
    : input #2, u
    :
    : if t <> u then
    : 'These files are differant
    : end if
    :
    : loop
    :
    : But it doesn't totally work. Do I need to use binary mode or something? How?
    :
    : Cheers
    :
    : Raz
    : ----------------------------------------------------
    : The solution to the problem changes the problem
    :

    VB code, not BASIC, but you know both so translate as needed:
    [code]
    Public Function CompFiles(ByVal File1 As String, ByVal File2 As String) As Boolean

    Dim FD1 As String
    Dim FD2 As String
    Dim FFile1 As Long
    Dim FFile2 As Long
    If FileLen(File1) <> FileLen(File2) Then
    Exit Function
    Else
    FFile1 = FreeFile
    Open File1 For Binary As FFile1
    FFile2 = FreeFile
    Open File2 For Binary As FFile2
    FD1 = String$(10240, 0)
    FD2 = FD1
    Do Until LOF(FFile1) - Seek(FFile1) + 1 < 10240
    Get FFile1, , FD1
    Get FFile2, , FD2
    If FD1 <> FD2 Then Exit Function
    Loop
    If Seek(FFile1) <= LOF(FFile1) + 1 Then
    FD1 = String$(LOF(FFile1) - Seek(FFile1) + 1, 0)
    FD2 = FD1
    Get FFile1, , FD1
    Get FFile2, , FD2
    If FD1 <> FD2 Then Exit Function
    End If
    CompFiles = True
    End IF

    End Function
    [/code]
    That should work. Grabs the file in 10K increments and compares it.

    MsgBox CompFiles("c:file.dat", "c:file1.dat")
  • Hadji_13Hadji_13 Member Posts: 274
    : : Hi
    : :
    : : I want to compaire two files,
    : :
    : : I've been using this code:
    : :
    : : Hi,
    : :
    : : This SHOULD work right. Right?
    : :
    : : Open "c:file.dat" for input as #1
    : : Open "c:file1.dat" for input as #2
    : :
    : : Do until eof(1)
    : :
    : : Input #1, t
    : : input #2, u
    : :
    : : if t <> u then
    : : 'These files are differant
    : : end if
    : :
    : : loop
    : :
    : : But it doesn't totally work. Do I need to use binary mode or something? How?
    : :
    : : Cheers
    : :
    : : Raz
    : : ----------------------------------------------------
    : : The solution to the problem changes the problem
    : :
    :
    : VB code, not BASIC, but you know both so translate as needed:
    : [code]
    : Public Function CompFiles(ByVal File1 As String, ByVal File2 As String) As Boolean
    :
    : Dim FD1 As String
    : Dim FD2 As String
    : Dim FFile1 As Long
    : Dim FFile2 As Long
    : If FileLen(File1) <> FileLen(File2) Then
    : Exit Function
    : Else
    : FFile1 = FreeFile
    : Open File1 For Binary As FFile1
    : FFile2 = FreeFile
    : Open File2 For Binary As FFile2
    : FD1 = String$(10240, 0)
    : FD2 = FD1
    : Do Until LOF(FFile1) - Seek(FFile1) + 1 < 10240
    : Get FFile1, , FD1
    : Get FFile2, , FD2
    : If FD1 <> FD2 Then Exit Function
    : Loop
    : If Seek(FFile1) <= LOF(FFile1) + 1 Then
    : FD1 = String$(LOF(FFile1) - Seek(FFile1) + 1, 0)
    : FD2 = FD1
    : Get FFile1, , FD1
    : Get FFile2, , FD2
    : If FD1 <> FD2 Then Exit Function
    : End If
    : CompFiles = True
    : End IF
    :
    : End Function
    : [/code]
    : That should work. Grabs the file in 10K increments and compares it.
    :
    : MsgBox CompFiles("c:file.dat", "c:file1.dat")
    :

    I still have problems with opening a file as binary.
    It comes up with the error type mismatch.
    And I had to rem out the conditions for the first if as that keyword doesn't exist in QB, But I presume that it will just find the differance later on in the file.

    Ok, a QB version would be appriciated, or more info on how to get binary mode working.

    Thanks
    Hadji
    -------------------------
    Nice to see someone's on the ball.
  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : It comes up with the error type mismatch.

    On which line?
  • Hadji_13Hadji_13 Member Posts: 274
    [b][red]This message was edited by Hadji_13 at 2002-11-2 6:4:56[/red][/b][hr]
    : : It comes up with the error type mismatch.
    :
    : On which line?
    :
    Open file1 for binary as what ever it was.
    Can't find any examples of opening a file in Binary mode in QB help

    It did work in VB though
    ----------------------------------------------------
    The solution to the problem changes the problem



  • Paul_WPaul_W Member Posts: 3
    I'm half asleep so I only half read your problem (sorry) but it reminded me of two things that almost drove me nuts. First I THINK the file name has to be in "quotation marks" or a string variable. SO it looks like:
    OPEN "file.dat" FOR BINARY AS 1
    or something similar. Second when you read from a binary file (and this is the one I'm REALLY not sure about) the string variable you load the data into has to be a set length. so when you dim it it looks like this:
    DIM thing AS STRING * 1
    I.e. no variable length string variables. I don't know how it works in input mode. I always use binary.

    Hope one or both of those things helps. all that makes sense to me but like I said I'm nearly asleep. It might look like garbage to you. If it does say so and I'll try to explain better. Hopefully while fully concious.
  • Andre YoungAndre Young USAMember Posts: 0

    ______ [ http://forcoder.org ] free ebooks and video tutorials about \ Visual Basic .NET, PL/SQL, Objective-C, C++, Go, Assembly, Scratch, C#, MATLAB, Visual Basic, R, C, JavaScript, PHP, Swift, Java, Ruby, Perl, Delphi, Python Rust, D, Kotlin, Lisp, Bash, Prolog, ML, Apex, Lua, Ada, Scala, Logo, SAS, Erlang, Crystal, Fortran, Scheme, Alice, Transact-SQL, VBScript, LabVIEW, F#, Clojure, Julia, FoxPro, COBOL, Dart, Hack, Awk, ABAP \ ___________

Sign In or Register to comment.