我需要颠倒文件的行序并将它们写到另一个文件中,但是我有一些问题。我不能在file2中写一些reason...Any建议和提示会有用,这是我遇到的第一个这种类型的问题。
示例:
输入file1:
line1
line 2
line 3所需的输出file2:
line 3
line2
line 1
.386
.model flat, stdcall
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;includem biblioteci, si declaram ce functii vrem sa importam
includelib msvcrt.lib
extern exit: proc
extern fopen:proc
extern getc:proc
extern fclose:proc
extern printf:proc
extern ftell:proc
extern fseek:proc
extern fscanf:proc
extern fprintf: proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;declaram simbolul start ca public - de acolo incepe executia
public start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;sectiunile programului, date, respectiv cod
.data
;aici declaram date
s db 99 dup(0)
read db "r",0
write db "w",0
nume db "fisier1.txt",0
nume2 db "fisier2.txt",0
seek_end dd 2
format db "%s",0
.code
start:
;open first file to read
push offset read
push offset nume
call fopen
add esp,8
mov esi,eax;save pointer of file
;open second file to write
push offset write
push offset nume2
call fopen
add esp,8
mov edi,eax;save pointer of file
;find the end of file
push seek_end
push -1
push esi
call fseek
add esp,12
;save in ecx current position
push esi
call ftell
add esp,4
mov ecx,eax
et:
push esi
call getc
add esp,4
cmp eax,0ah;verify if isn't new line
jne previous
previous:
;move to the previous line
push 1
push -1
push esi
call fseek
add esp,12
jmp cont
read_write:
;read the line in string s
push offset s
push offset format
push esi
call fscanf
add esp,12
;print string s in second file
push offset s
push offset format
push edi
call fprintf
add esp,12
jmp previous
cont:
dec ecx
;verify if isn't the beginning of file
cmp ecx,0
jne et
push 0
call exit
end start发布于 2018-05-07 07:04:41
这绝对不是我用汇编语言写的东西……但是,为了完成这项工作,我首先要用高级语言编写算法。如果你能让逻辑在更高级的语言中正常工作,你就可以让你的汇编语言工作。
in = fopen("source.txt", "r");
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
out = fopen("destination.txt", "w");
ftruncate(out, size);
fseek(out, 0, SEEK_END);
while(fgets(buf, sizeof(buf), in))
{
len = strlen(buf);
fseek(out, -len, SEEK_CUR);
fwrite(out, 1, len, buf);
fseek(out, -len, SEEK_CUR);
}此函数对由sizeof(buf)确定的行的大小有限制,这一行中有一个错误:
如果
fgets()可以读取sizeof(buf)字节,则它不会返回以null结尾的字符串。
不幸的是,这就是C库有多糟糕。一个简单的bug修复方法:
buf[sizeof(buf) - 1] = '\0';
...
fgets(buf, sizeof(buf) - 1, in)也就是说,您将'\0'放在缓冲区的末尾,并且永远不会覆盖它。所以至少你不会溢出你的缓冲区。
现在让你在汇编语言中转换C代码。
另外,在进行实际编码之前,首先要理解你的算法。
注意:我也没有测试任何错误代码。错误处理是一个好主意。(例如,如果fopen("destination.txt"...)失败了怎么办?)
https://stackoverflow.com/questions/50203188
复制相似问题