从输入文件中读取,检查其中的一些信息并将其存储在C中的数组中
我有如下输入文件:从输入文件中读取,检查其中的一些信息并将其存储在C中的数组中,c,arrays,fopen,fgets,C,Arrays,Fopen,Fgets,我有如下输入文件: The Branch PC is e05f56f8 The Branch Type is branch_type_1 The Branch is Taken The Branch Target is e045ac20 jal__ 1141512 我需要从行“分支PC为******”中获取分支PC的信息,以及是否从行“分支已采取/未采取”中获取分支。并将这两个信息存储到所有分支的二维数组中 我想知道在我使用fopen之后,如何检查每一行以获得所需的
The Branch PC is e05f56f8
The Branch Type is branch_type_1
The Branch is Taken
The Branch Target is e045ac20
jal__ 1141512
我需要从行“
分支PC为******
”中获取分支PC的信息,以及是否从行“分支已采取/未采取”中获取分支。并将这两个信息存储到所有分支的二维数组中
我想知道在我使用fopen
之后,如何检查每一行以获得所需的信息。您可以一直阅读,直到检测到行终止符,如\r\n或\n,具体取决于windows和Linux。然后将n个字符读入每行中,并将其读入行终止符。我想您需要的是:
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);
解析后存储到数组应该不会有问题。我想如果您使用一些bash脚本对文本文件执行预处理,然后让C程序处理脚本的输出会容易得多。谢谢。它解决了我的问题!
The Branch PC is e05f57fc
The Branch Type is branch_type_1
The Branch is Taken
The Branch Target is e05f578c
jal__ 1562083
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);