已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。
4天前关闭。
Improve this question
我有一个c++中的void *
,它包含很多日志,但是我们不知道每个日志的长度。如何将其拆分为char*
,使用正则表达式或其他方法。
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <stdlib.h>
int main()
{
void *logs = malloc(1024);
char eachLog[100][100];
if (logs == NULL) {
printf("malloc failed\n");
return -1;
}
memcpy(logs, "2023/05/29 10:12:16 638377 [ debug] this is\n 1st log\n", 53);
memcpy((char*)logs + 53, "2023/05/29 10:12:16 638378 [ err] this is 2st log\n", 52);
memcpy((char*)logs + 105, "2023/05/29 10:12:16 638379 [ info] this is 3th log\n", 52);
/* the logs may be like this:
2023/05/29 10:12:16 638377 [ debug] this is\n 1st log\n2023/05/29 10:12:16 638378 [ err] this is 2st log\n2023/05/29 10:12:16 638379 [ info] this is 3th log\n
What I want is:
put the 1st log to eachLog[0];
put the 2st log to eachLog[1];
put the 3st log to eachLog[2];
*/
free(logs);
logs = NULL;
return 0;
}
1条答案
按热度按时间xlpyo6sf1#
在C++代码中,读取像您这样的日志文件(带有零散的换行符)看起来像这样(没有void*,没有显式的手动内存管理,这都是由std::string & std::vector完成的)
在线演示:https://onlinegdb.com/peAvoAb0k