Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从输入文件中读取,检查其中的一些信息并将其存储在C中的数组中_C_Arrays_Fopen_Fgets - Fatal编程技术网

从输入文件中读取,检查其中的一些信息并将其存储在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);