Search code examples
carraysfopenfgets

Read from an input file, check and store some of it information in arrays in C


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.

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


Solution

  • 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..