gpt4 book ai didi

C 查找替换不起作用

转载 作者:行者123 更新时间:2023-11-30 17:37:37 27 4
gpt4 key购买 nike

这是不起作用的功能:

char * insert_symtab(char *buf)
{
char *ret = (char *)malloc(strlen(buf) + 512);
char *tmp = NULL;
char *repl = NULL;
char obuf[32]; //for converting int to hex
Symtab_struct *cursym;
lineQueue *lq = readlines(buf);
int i;
strcpy(ret, "");
while (!lq->empty())
{
tmp = getlinefromqueue(lq);
for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
{
sprintf(obuf, "0x%04x", cursym->sym_location);
//printf("-->looking for %s\n", cursym->sym_name);
if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
{
printf("---->Found %s\n", cursym->sym_name);
strncpy (repl, obuf, strlen(cursym->sym_name));
}
}
strcat(ret, tmp);
strcat(ret, "\n");
}
return ret;
}

当这样运行时,我得到以下输出:

[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
$start

而且,如果我进去换衣服

if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
to
if ((repl = strstr(tmp, "$start")) != NULL)

我得到以下输出:

[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
---->Found $start
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
0x0000

应该如此。附件是整个项目的 Pastebin 链接:

main.cpp: http://pastebin.com/AiDCbsCt

Symtab.h:http://pastebin.com/Kmcn6NzV

Symtab.cpp:http://pastebin.com/mxtPL1d2

有什么想法吗?

最佳答案

发现并纠正问题

            for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
strncpy(sym_name_tmp, tmp + 1, i - 1); //grab the symbol

需要更改为

            for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
strncpy(sym_name_tmp, tmp + 1, i); //grab the symbol
*strstr(sym_name_tmp, ":") = 0; //this fixes teh 0x7f issue

关于C 查找替换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340186/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com