C++ yaml 解析二位数组
时间: 2023-02-16 07:51:34 浏览: 138
在 C 语言中,可以使用 libyaml 库来解析 YAML 文件。
下面是一个示例代码,展示了如何解析 YAML 文件中的二维数组:
```
#include <stdio.h>
#include <yaml.h>
int main() {
yaml_parser_t parser;
yaml_token_t token;
/* Initialize parser */
if(!yaml_parser_initialize(&parser))
fputs("Failed to initialize parser!\n", stderr);
if(parser.error)
fprintf(stderr, "Parser error %d\n", parser.error);
/* Set input file */
FILE *input = fopen("input.yaml", "rb");
yaml_parser_set_input_file(&parser, input);
/* Read the next token */
yaml_parser_scan(&parser, &token);
if(token.type == YAML_STREAM_START_TOKEN) {
/* Read the next token */
yaml_parser_scan(&parser, &token);
if(token.type == YAML_DOCUMENT_START_TOKEN) {
/* Read the next token */
yaml_parser_scan(&parser, &token);
if(token.type == YAML_SEQUENCE_START_TOKEN) {
/* Read the next token */
yaml_parser_scan(&parser, &token);
/* Read the elements of the sequence */
while(token.type != YAML_SEQUENCE_END_TOKEN) {
/* Process the element */
if(token.type == YAML_SEQUENCE_START_TOKEN) {
/* Read the next token */
yaml_parser_scan(&parser, &token);
/* Read the elements of the inner sequence */
while(token.type != YAML_SEQUENCE_END_TOKEN) {
/* Process the element */
if(token.type == YAML_SCALAR_TOKEN) {
printf("%s\n", token.data.scalar.value);
}
/* Read the next token */
yaml_parser_scan(&parser, &token);
}
}
/* Read the next token */
yaml_parser_scan(&parser, &token);
}
}
}
}
/* Clean up */
yaml_token_delete(&token);
yaml_parser_delete(&parser);
fclose(input);
return 0;
}
```
在这段代码中,我们首先初始化了解析器,然后设置了输入文
阅读全文