从输入文件中读取,检查并将其中的一些信息存储在C中的数组中

时间:2021-11-27 21:23:32

I've got input files like these:

我有这样的输入文件:

The Branch PC is e05f56f8

The Branch Type is branch_type_1

The Branch is Taken

The Branch Target is e045ac20

jal__        1141512 


The Branch PC is e045ac44

The Branch Type is branch_type_1

The Branch is Taken

The Branch Target is e05f57d4

jal__        1562101


The Branch PC is e05f57fc

The Branch Type is branch_type_1

The Branch is Taken

The Branch Target is e05f578c

jal__        1562083

I need to get the information of the branch PC from line "The Branch PC is ********", and whether the branch is Taken for Not Taken from line "The Branch is Taken/Not Taken". And store this two information into a 2-D array for all the Branches.

我需要从“分支PC是********”这一行获取分支PC的信息,以及是否从“分支被采取/未采取”这一行获取未采取的分支。并将这两个信息存储到所有分支的二维数组中。

I was wondering after I use fopen, how can I check each line to get the information I want.

我在使用fopen之后想知道,我如何检查每一行以获取我想要的信息。

3 个解决方案

#1


0  

I guess you want something like:

我想你想要的东西:

char hm[20], line[128];
FILE *fd=fopen("lol", "r");
while(fgets(line, 128, fd)) {
    if(strstr(line, "PC")) sscanf(line, "The Branch PC is %s\n", &hm);
    if(strstr(line, "Not Taken")) printf("%s Not taken !\n", hm);
    else if(strstr(line, "Taken")) printf("%s Taken !\n", hm);
}
fclose(fd);

Storing into array shouldn't be a problem after parsing..

解析后存储到数组应该不是问题。

#2


0  

You can read until a line terminator is detected like \r\n or \n depending on windows vs Linux. Then read n characters into each line to the line terminator.

您可以读取直到检测到行终止符,如\ r \ n或\ n,具体取决于Windows与Linux。然后在每行中读取n个字符到行终止符。

#3


0  

I suspect it would be a lot easier if you use some bash scripting to perform a preprocessing of your text file, then let the C program work over the output of the script

我怀疑如果你使用一些bash脚本来执行文本文件的预处理会更容易,然后让C程序在脚本的输出上工作

#1


0  

I guess you want something like:

我想你想要的东西:

char hm[20], line[128];
FILE *fd=fopen("lol", "r");
while(fgets(line, 128, fd)) {
    if(strstr(line, "PC")) sscanf(line, "The Branch PC is %s\n", &hm);
    if(strstr(line, "Not Taken")) printf("%s Not taken !\n", hm);
    else if(strstr(line, "Taken")) printf("%s Taken !\n", hm);
}
fclose(fd);

Storing into array shouldn't be a problem after parsing..

解析后存储到数组应该不是问题。

#2


0  

You can read until a line terminator is detected like \r\n or \n depending on windows vs Linux. Then read n characters into each line to the line terminator.

您可以读取直到检测到行终止符,如\ r \ n或\ n,具体取决于Windows与Linux。然后在每行中读取n个字符到行终止符。

#3


0  

I suspect it would be a lot easier if you use some bash scripting to perform a preprocessing of your text file, then let the C program work over the output of the script

我怀疑如果你使用一些bash脚本来执行文本文件的预处理会更容易,然后让C程序在脚本的输出上工作