我有一个.txt文件,其中包含一些数字,例如5123453479
。
必须从该文件中读取它们,然后找到它们的总和。我的文件读取代码工作正常。问题是我无法从该缓冲区中获取某个元素。假设我要从编号为2的缓冲区(5123453479
)中获取索引2。
lea edx, buffer[2]
push edx
call StdOut
上面的代码返回123453479,而不仅仅是2
。
这是整个代码:
include \masm32\include\masm32rt.inc
Main PROTO
.data
newLine db 10, 13, 0
txtFilter db "*.txt", 0
txtFD WIN32_FIND_DATA <>
txtHandle HANDLE ?
fHandle HANDLE ?
bufferLength dd ?
buffer db 5000 dup(?)
lnt dd 1024
error db "Error!", 0
.code
start:
invoke Main
invoke ExitProcess, 0
Main proc
; Find first .txt file in current directory
push offset txtFD
push offset txtFilter
call FindFirstFile
mov txtHandle, eax
; Print current file name
push offset txtFD.cFileName
call StdOut
; Print a newline
push offset newLine
call StdOut
; Open file
push 0 ; HANDLE hTemplateFile
push FILE_ATTRIBUTE_NORMAL ; DWORD dwFlagsAndAttributes
push OPEN_EXISTING ; DWORD dwCreationDisposition
push 0 ; LPSECURITY_ATTRIBUTES lpSecurityAttributes
push 0 ; DWORD dwShareMode
push GENERIC_READ ; DWORD dwDesiredAccess
push offset txtFD.cFileName ; LPCTSTR lpFileName,
call CreateFile
.if eax == INVALID_HANDLE_VALUE
jmp _error
.else
mov fHandle, eax
.endif
push 0 ; LPOVERLAPPED lpOverlapped
push offset bufferLength ; LPDWORD lpNumberOfBytesRead
push lnt ; DWORD nNumberOfBytesToRead
push offset buffer ; LPVOID lpBuffer
push fHandle ; HANDLE hFile
call ReadFile
jmp _next
_error:
push offset error
call StdOut
jmp _next
_next:
; Print text from file
push offset buffer
call StdOut
; Print a newline
push offset newLine
call StdOut
; Faulty code here
lea edx, buffer[2]
push edx
call StdOut
; Close a file handle
push fHandle
call CloseHandle
; Close a handle
push txtHandle
call FindClose
ret
Main endp
end start
输出:
test.txt
5123453479
123453479
buffer[2]
是错误的。当前,我只想按数字枚举缓冲区号,所以最后我可以对数字求和。我该怎么办?