开发者

VB6 not desired result reading c:\test.txt

开发者 https://www.devze.com 2023-01-11 06:23 出处:网络
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 \"su

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???


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]


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


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?


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消