Archive: strcmp problem


strcmp problem
I run this function(writenewvars) to check for a string on a line number in a some text files...strcmp seems to not funtion correctly,can some one point out my error?

this function(writenewvars) faults at strcmp.

hmm cant upload an attachment so i have to do this way...

Function ReadFileLine
Exch $0 ;file
Exch
Exch $1 ;line number
Push $2
Push $3

FileOpen $2 $0 r
StrCpy $3 0

Loop:
IntOp $3 $3 + 1
ClearErrors
FileRead $2 $0
IfErrors +2
StrCmp $3 $1 0 loop
FileClose $2

Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd
(by AfrowUK)

Function WriteToFileLine
Exch $0 ;file
Exch
Exch $1 ;line number
Exch 2
Exch $2 ;string to write
Exch 2
Push $3
Push $4
Push $5
Push $6
Push $7

GetTempFileName $7
FileOpen $4 $0 r
FileOpen $5 $7 w
StrCpy $3 0

Loop:
ClearErrors
FileRead $4 $6
IfErrors Exit
IntOp $3 $3 + 1
StrCmp $3 $1 0 +3
FileWrite $5 "$2$\r$\n$6"
Goto Loop
FileWrite $5 $6
Goto Loop
Exit:

FileClose $5
FileClose $4

SetDetailsPrint none
Delete $0
Rename $7 $0
SetDetailsPrint both

Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd
(by AfrowUK)

(my Function)
Function writenewvars

IfFileExists "$INSTDIR\myfile.txt" go next
go:
Push 25 ;line number to read from
Push "$INSTDIR\myfile.txt" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
strcmp $0 "" write next
write:
Push "my string"
Push 25
Push "$INSTDIR\myfile.txt"
Call WriteToFileLine
goto next
next:
IfFileExists "$INSTDIR\myfile2.txt" go1 next1
go1:
Push 25 ;line number to read from
Push "$INSTDIR\myfile2.txt" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
strcmp $0 "" write1 next1
write1:
Push "my string"
Push 25
Push "$INSTDIR\myfile2.txt"
Call WriteToFileLine
goto next1
next1:
IfFileExists "$INSTDIR\myfile3.txt" go2 next2
go2:
Push 25 ;line number to read from
Push "$INSTDIR\myfile3.txt" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
strcmp $0 "" write2 next2
write2:
Push "my string"
Push 25
Push "$INSTDIR\myfile3.txt"
Call WriteToFileLine
goto next2
next2:
IfFileExists "$INSTDIR\myfile4.txt" go3 next3
go3:
Push 25 ;line number to read from
Push "$INSTDIR\myfile4.txt" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
strcmp $0 "" write3 next3
write3:
Push "my string"
Push 25
Push "$INSTDIR\myfile4.txt"
Call WriteToFileLine
next3:

functionend

what happens is that the code fails to run after the strcmp line ,it continues to the next function regardless of labels...even though dumpstate reports $0 as expected "my string" is read but the labels are ignored.As a matter of fact as long as there is a [is not equal] label the code after strcmp does not run at all(i can test this by running dumpstate or messagebox command)is equal code is ran regardless...relative jumps perform the same way...it writes "my string" regardless what is stored in $0.I think my code is correct and this is a strcmp problem.


Nope. Code problem.

Your code is rather long too, you could cut it down greatly:


!macro WriteNewVars String File Number
IfFileExists "${File}" 0 Next_${Number}
Push 25 ;line number to read from
Push "${File}" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
Push $0
Call TrimNewLines
Pop $0
StrCmp $0 "" 0 Next_${Number}
Push "${String}"
Push 25
Push "${File}"
Call WriteToFileLine
Next_${Number}:
!macroend

Function WriteNewVars
!insertmacro WriteNewVars "my string" "$INSTDIR\myfile1.txt" "1"
!insertmacro WriteNewVars "my string" "$INSTDIR\myfile2.txt" "2"
!insertmacro WriteNewVars "my string" "$INSTDIR\myfile3.txt" "3"
!insertmacro WriteNewVars "my string" "$INSTDIR\myfile4.txt" "4"
FunctionEnd


Notice the new function call: TrimNewLines.
You were comparing $0 to "" when $0 would have been $\r$\n (= a blank line). TrimNewLines will remove $\r$\n so $0 becomes "". You can get the extra function from the archive.

-Stu

wow I R TEH NEWB :) thxz alot afrow for your time to help,works great,and I learned some things.