samedi 18 mai 2019

Regex problem: Why my code doesn't run properly?

I'm facing a really weird problem. I'm trying to read in the input from a file, then use regular expression to find and print the result out on screen. My file contains carts and the product's name. The file has thousands of line, so here's a quick example of the file:

Cart561
219328518230,0B6060D038C0,819303200230,81E068407C08,A44323118230,A44488643848,8484C2D09A40,A1460324CA40,
Cart210
834248C52248,8490CB1068C0,
Cart2460B41A124C218,06C1A12528C0,864249906620,2344B20C4230,291329103218,8490CB1068C0,2370231180B0,294621306230
... (More below)

When I try to read and print out only the first 5 or 10 carts, my code runs properly. However, when I try to read and print out all the carts and names, for some reason it doesn't start from the beginning. Instead, it jumps to the middle of the file and starts processing there.

Here's my code:

#include <iostream>
#include <regex>
#include <fstream>

using namespace std;

int main()
{
    ifstream cart("Carts.csv");
    regex h("[0-9A-Z]{12}");
    smatch n;
    int count = 0;

    while (getline(cart, line))
    {
        // If count is an even number, the compiler will print out the cart's name
        if (count % 2 == 0)
            cout << line << endl;
        while (regex_search(line, n, h))
        {
            for (auto x : n)
                cout << x << endl;
            line = n.suffix().str();
        }
        count++;
        cout << count << endl;  // This one just for my test
    }

If I add this to the while loop to make it only print out 5 first results:

if(count==5) {break;}

The code runs fine. But if I remove that code, and let the compiler read and print the whole file, it doesn't print the first half of the file, it starts in the middle of nowhere like this:

A444B0246A40
2305A3109818
24E060D06260
819302D48860
29460A4528C0
09C24A503260
0B3058819260
8484E910C890
234260C19260
60C629016890
8910C98C18C0
0B60492000B0
234270246848
864482718C80
06C260C43260
2344B0306620
A44322548890
42 -> This is number of line in file
Cart277
43 -> This is number of line in file
A44482C16848
294482D090B0
2652230C4A40
0CC629112830
896408C168C0
06C1A12528C0
86424864C248
864248D0C620
E0424B058218
2910F0342230
2C10E2446230
2490F0318620
A441A244C230
8490CB1068C0
0B6060D038C0
A44242616848
0B44898438C0
81C4A1843830
234270246848
8484C2D09A40
44 -> This is number of line in file
Cart287
45 -> This is number of line in file
... (More below)

After testing, I realize it somehow ignore my 41 first lines. The count still counts from the beginning but the compiler only prints out the result from the middle of the 42th line. (If you want to know, there's a total of 1994 lines in the file)

In short, I still don't know what exactly is my problem and what should I do now to fix this. Do you have any suggest? I'm very appreciate if any of you can explain to me why this happened.

P/s: Since this is part of my homework assignment, I must use regular expression.

Aucun commentaire:

Enregistrer un commentaire