Exist
Exist

Reputation: 87

VB6 not desired result reading c:\test.txt

I start out with text103.text having a text value of what I want to check C:\test.txt for. So if whatever's in text103.text matches up with whatever's in C:\test.txt label3.caption should read "success" yet every time I run it, I get "failure" why??

So here's the code in my button:

Private Sub Command1_Click()

    nFileNum = FreeFile
    Open "C:\test.txt" For Input As nFileNum
    lLineCount = 1

    Do While Not EOF(nFileNum)
       Line Input #nFileNum, sNextLine
       sNextLine = sNextLine & vbCrLf
       sText = sText & sNextLine
    Loop
    Text102.Text = sText
    Close nFileNum


    If Text102.Text = Text103.Text Then
        Label3.Caption = "success"
    Else
        Label3.Caption = "failure"
    End If


End Sub

Even when my text103.text starts out as "hello" and I edit the C:\test.txt to just say "hello" it always gives me label3.caption "failure"!!! Why???

Upvotes: 1

Views: 244

Answers (5)

Binary Worrier
Binary Worrier

Reputation: 51711

Possibly because you are always adding a newline to the data read from the file.
Does Text103.Text contain a new line too?

Update:

vbCrLf aka \r\n are part of the set of whitespace characters so you may not be able to see them directly.

Before If Text102.Text = Text103.Text Then try
msgbox "Len 102 " & Len(Text102.Text) & " Len 103 " & Len(Text103.Text)
this will show that the strings are different lengths, therefore they cannot be equal.

Alternatively, in immediate mode try ? "[" & text102.Text & "]" and ? "[" & text103.Text & "]" Assuming the word in question is "Hello", I'll bet the first will print

[Hello
]

and the second
[Hello]

Upvotes: 5

Paul Michaels
Paul Michaels

Reputation: 16685

Try this:

Private Sub Command1_Click()

nFileNum = FreeFile
Open "C:\test.txt" For Input As nFileNum
lLineCount = 1

Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   sNextLine = sNextLine & vbCrLf
   sText = sText & sNextLine
Loop
Text102.Text = sText
Close nFileNum


If Replace$(Text102.Text, VBCrLf, "") = Replace$(Text103.Text, VbCrLf, "") Then
    Label3.Caption = "success"
Else
    Label3.Caption = "failure"
End If


End Sub

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 54999

I'd make a guess that it's to do with the newlines (vbCrLf) that you add, or some similar character.

Otherwise it might be case dependant, you could try adding Option Compare Text at the top of the file.

Upvotes: 2

Chris
Chris

Reputation: 27599

Could it be to do with your trailing carriage return? It looks like your file read will always have a vbCrLf on the end of it whereas possibly your text103 doesn't. Can you go into debug mode and confirm exactly what each string contains?

Upvotes: 2

Radu
Radu

Reputation: 8699

It's because you are adding newline characters text103.text does not have this.

Upvotes: 2

Related Questions