Why is my regex function only running the first time around?

Ben Moore

I'm working on some code for a homework assignment which is to search a text file for certain patterns and generate reports. Currently to test it I'm just printing my reports to the screen. But my search function only seems to be running the first time. I've tested the regex individually and they do all pull the correct matches but once I put it inside the function it only works the first time it's run. Could anyone explain to me why this is happening?

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>


// Report expressions
char *reports[5] = {"^ {0,}[0-9]{1,4}", "S", "L", "S {0,1}(1|2)", "^ {0,}[0-9]{1,4} {1,} L | 3$"};

void search(FILE *fp, char *report, int index) {
        regex_t reg;
        char buf[256];
        int reti = regcomp(&reg,report,REG_EXTENDED);

        if(reti) {
                printf("Regex compilation failed, noob\n");
                exit(1);
        }
        printf("Report %d\n", index);

        while(fgets(buf,sizeof(buf),fp) != NULL) {
                //printf("%s",buf);
                reti = regexec(&reg,buf,0,NULL,0);
                if(!reti) { //if there's a match
                        printf("%s",buf);
                } else if(reti == REG_NOMATCH) {
                        printf("No match\n");
                }
        }

        regfree(&reg);
}

int main(void) {
        FILE *fp;

        fp = fopen("./Hammer.data","r");

        if(fp == NULL) {
                perror("Error opening file");
                return(-1);
        }

        for(int i = 0;i < 5;i++) {
            search(fp, reports[i],i+1);
        }

        fclose(fp);

        return(0);
}
kaylum
while(fgets(buf,sizeof(buf),fp) != NULL) 

That will result in the fp pointing to the end of the file. So the next time the function is called the fgets will immediately return NULL. One fix is to rewind the file pointer before the fgets loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is only my first HTTP request running?

From Dev

Function not firing first time around

From Dev

Why is my list function only outputting first value of my array?

From Dev

Why is my function running without asking for the input first?

From Dev

why setTimeout() only run my code once,at first time?

From Dev

Why will my QRCode scanner / regex strip only work 70% of the time?

From Dev

Why does my function only returns the first object from the array

From Dev

Angular JS form running only first time

From Dev

Update cart item quantity with ajax only works first time around

From Dev

why is my function not adding up all my numbers? it only gives the first number

From Dev

Function is running multiple time as per CPU core(CPU cores = 4 in my case). How can I run function only 1 time?

From Dev

Why does my removeDuplicates method only remove the duplicate integer the first time it encounters it?

From Dev

Why are Scala Futures running only 2 at a time?

From Dev

Why setBackground works only the first time? (JPanel)

From Dev

Why read function works only the first time we open the object in python?

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

vim function only works properly the first time

From Dev

onClick function only works for the first time in react?

From Dev

jquery .load() function only working the first time

From Dev

Function works correctly only the first time it is called

From Dev

How to call function only for first time in loop

From Dev

Why is my read function throwing away the first char when calling it a second time?

From Dev

My function always returns false the first time

From Dev

Why are my promises (running in parallel and running in serial) completing at the same time

From Dev

Why my function is running before window resizes?

From Dev

why only first letter is returned by match function?

From Dev

Why JQuery function works for only the first textbox

Related Related

  1. 1

    Why is only my first HTTP request running?

  2. 2

    Function not firing first time around

  3. 3

    Why is my list function only outputting first value of my array?

  4. 4

    Why is my function running without asking for the input first?

  5. 5

    why setTimeout() only run my code once,at first time?

  6. 6

    Why will my QRCode scanner / regex strip only work 70% of the time?

  7. 7

    Why does my function only returns the first object from the array

  8. 8

    Angular JS form running only first time

  9. 9

    Update cart item quantity with ajax only works first time around

  10. 10

    why is my function not adding up all my numbers? it only gives the first number

  11. 11

    Function is running multiple time as per CPU core(CPU cores = 4 in my case). How can I run function only 1 time?

  12. 12

    Why does my removeDuplicates method only remove the duplicate integer the first time it encounters it?

  13. 13

    Why are Scala Futures running only 2 at a time?

  14. 14

    Why setBackground works only the first time? (JPanel)

  15. 15

    Why read function works only the first time we open the object in python?

  16. 16

    Why my listview only detects the first checkbox?

  17. 17

    Why is my for only working on the first variable?

  18. 18

    Why is my for loop only grabbing first element?

  19. 19

    vim function only works properly the first time

  20. 20

    onClick function only works for the first time in react?

  21. 21

    jquery .load() function only working the first time

  22. 22

    Function works correctly only the first time it is called

  23. 23

    How to call function only for first time in loop

  24. 24

    Why is my read function throwing away the first char when calling it a second time?

  25. 25

    My function always returns false the first time

  26. 26

    Why are my promises (running in parallel and running in serial) completing at the same time

  27. 27

    Why my function is running before window resizes?

  28. 28

    why only first letter is returned by match function?

  29. 29

    Why JQuery function works for only the first textbox

HotTag

Archive