这样做有点奇怪,它只是部分起作用:
...
StrCmp ${res} "" 0 +2 ; +1 and 0 is the same, jumps to next instruction
StrCpy ${res} 1 ; No need to do math here
IntOp ${res} ${res} + 0 ; NaN + 0 = 0 but what if you read a number from the file?
如果文件可能以数字开头,您需要像 zbynour 建议的那样跳转:
...
StrCmp ${res} "" 0 +3
StrCpy ${res} 1
Goto +2
StrCpy ${res} 0
如果你翻转测试,你可以用相同数量的指令得到你想要的:
!macro IsFileNOTEmpty fName res
!insertmacro ReadFile "${fName}" ${res}
StrCmp ${res} "" +2 0
StrCpy ${res} 1 ; Or IntOp ${res} ${res} | 1 if you really wanted to do extra math ;)
IntOp ${res} ${res} + 0
!macroend
甚至更好:
!macro IsFileEmpty fName res
!insertmacro ReadFile "${fName}" ${res}
StrLen ${res} ${res} ; If this is enough depends on how you want to deal with newlines at the start of the file (\r or \n)
!macroend
...所有这些代码都假定您要测试文件是否以某些文本开头,如果文件以 0 字节开头,它将始终说文件为空。因此,如果文件包含二进制内容,则需要使用 Francisco R 的代码来测试实际大小(以字节为单位)。