我有一个非常巨大的 json 对象(使用名为 espree 的 JavaScript 解析器创建,包含一个对象数组)。我想将其写入 .json 文件,但每次都会因内存分配问题而失败(我的堆大小为 22 Gb)。
据我了解,当数据未写入文件时缓冲区会过载。
如果我仅使用同步文件操作,输出将写入文件,但应用程序的运行时间会扩展。
我尝试过但失败的解决方案(尝试序列化整个对象,然后尝试序列化数组的项目):
这是当前的代码:
const bjson= require('big-json');
function save(result) {
let outputStream = fs.createWriteStream(/*path*/);
const stringifyStream = bjson.createStringifyStream({
body: result
});
function write(d) {
let result = outputStream.write(d);
if (!result) {
outputStream.once('drain', write);
}
}
stringifyStream.on('data', function (chunk) {
writeData(chunk);
});
stringifyStream.on('end', function () {
outputStream.end();
});
}
let results = [/*results as an array, containing lots of json objects*/];
for (let i = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。
所以这个正在工作:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个既不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach); …Run Code Online (Sandbox Code Playgroud)