Alisson Linneker
Alisson Linneker

Reputation: 292

While file data with regex

I have problem on while file data on get only regular expession. I would get only the sorted data table.

My file:

Foods list test.
Check all foods:
1   123456   Food Name 1
2   123457   Food Name 2
3   123458   Food Name 3
4   123459   Food Name 4
5   123460   Food Name 5
blablabla blablabla
...
file foods done

ID\tCOD\t(SPACE)NAME

My code:

int id, cod;
char name[64];
FILE *file;
file = fopen("foods.txt", "r")
while(fscanf(file, "(\d+)\t(\d+)\t (.*)", &id, &cod, name) != EOF)
    printf("ID: %d - Code: %d - Name: %s\n", id, cod, nome);

Is not working as it should, why? You are entering an endless loop.

Upvotes: 2

Views: 49

Answers (1)

dbush
dbush

Reputation: 224577

The scanf function does not accept regular expressions. It has its own syntax similar to printf.

Try this instead:

while(fscanf(file, "%d %d %s", &id, &cod, name) == 3)

Upvotes: 3

Related Questions