gpt4 book ai didi

调用我的函数,导致程序崩溃 - C | 3DS Homebrew 软件

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:48 25 4
gpt4 key购买 nike

我一直在为 Nintendo 3DS 编写一个 Homebrew 程序,它是用 C 编写的。它只是解析一个 JSON 文件并将其打印到屏幕上。问题是在解析并在屏幕上打印后,它就崩溃了。

代码:

char JSON_FILE[] = "jsbr-ford-mustang.json";
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}

const char * parse_json(char* value) {
u8* file_buffer; FILE *file = fopen(JSON_FILE,"rb"); if (file == NULL) printf("Error.\n");
fseek(file,0,SEEK_END); off_t size = ftell(file); fseek(file,0,SEEK_SET); file_buffer=malloc(size);
if(!file_buffer) printf("Error.\n");
off_t bytesRead = fread(file_buffer,1,size,file); fclose(file);
if(size!=bytesRead) printf("Error.\n");
int i; int r;
jsmn_parser p; jsmntok_t t[128]; jsmn_init(&p);
r = jsmn_parse(&p, file_buffer, size, t, sizeof(t)/sizeof(t[0]));

if (r < 0) {
printf("Failed to parse JSON: %d\n", r);
return 1;
}
if (r < 1 || t[0].type != JSMN_OBJECT) {
printf("Object expected\n");
return 1;
}
printf("Debug START\n");
for (i = 1; i < r; i++) {
if (jsoneq(file_buffer, &t[i], value) == 0) {
printf("Debug 1\n");
break;
}
printf("Debug 2\n");
}
printf("Debug 3\n");
return printf("%.*s\n", t[i+1].end-t[i+1].start, file_buffer + t[i+1].start);
}

int main() {
gfxInitDefault();
consoleInit(GFX_TOP,NULL);
printf("P1\n");
printf("Description: %s",parse_json("description"));
printf("P2\n");
printf("Sync spacing: %s",parse_json("synchronization_spacing_us"));
while (aptMainLoop()) {
hidScanInput(); u32 kDown = hidKeysDown();
if(kDown & KEY_START) {
consoleClear();
break;
}
gfxFlushBuffers();
gfxSwapBuffers();
}
gfxExit();
return 0;
}

调试输出:

P1
Debug START
Debug 2
Debug 2
Debug 1
Debug 3
Ford Mustang, 40MHz, No. 23019

这是一段视频:https://www.youtube.com/watch?v=zpt_BEMyIOc

这是我拥有的 GitHub 存储库:https://github.com/lavanoid/3DS_JSON_Parser

最佳答案

来自 cplusplus.com关于 printf() 的返回值:

On success, the total number of characters written is returned.

If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

你不能这样做:return printf("%.*s\n", t[i+1].end-t[i+1].start, file_buffer + t[i+1].开始);

因为您的函数需要返回 const char *,而不是 int,它是从 printf() 返回的。

您应该使用 sprintf() 将您想要的字符串打印到缓冲区中(例如 tempString)

static char *tempString;
...
if(tempstring != NULL) {
free(tempString);
tempString = NULL;
}
tempString = (char *)malloc(t[i+1].end-t[i+1].start+1);
if(tempString != NULL) {
sprintf(tempString,"%s", file_buffer + t[i+1].start);
return (const char *)tempString;
} else {
return "malloc error!";
}

关于调用我的函数,导致程序崩溃 - C | 3DS Homebrew 软件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32234631/

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