mardi 19 juillet 2022

Trying to count the number of lines in a file but it returns other numbers? [closed]

I've been trying to count the number of lines within a file as such:

regex cityLocationRegex (R"(/(\[(\d+).*\s(\d+)]-(\d+)-(.+)*))");
regex cloudCoverRegex (R"(\[(\d+), (\d+)]-(\d+))");
regex pressureRegex (R"(\[(\d+), (\d+)]-(\d+))");

// to open the file & count the number of lines within the file

// citylocation.txt
ifstream inputCLFile; 
inputCLFile.open(cityLocationFile.c_str());
if (inputCLFile.is_open())
{
    while (getline (inputCLFile, fileline))
    {
        if (regex_search(fileline, match, cityLocationRegex))
        {
            cityLocationCount++;
        }
    }
    }
inputCLFile.close();

// cloudcover.txt
ifstream inputCCFile; // input file stream
inputCCFile.open(cloudCoverFile.c_str());
if (inputCCFile.is_open())
{
    while (getline (inputCCFile, fileline))
    {
        if (regex_search(fileline, match, cloudCoverRegex))
        {
            cloudCoverCount++;
        }
    }
}
inputCCFile.close();    
    
// pressure.txt
ifstream inputPressureFile; // input file stream
inputPressureFile.open(pressureFile.c_str());
if (inputPressureFile.is_open())
{
    while (getline (inputPressureFile, fileline))
    {
        if (regex_search(fileline, match, pressureRegex))
        {
            pressureCount++;        
        }
    }
}
inputPressureFile.close();



cout << "City location count: " << cityLocationCount << endl;
cout << "Cloud cover count: " << cloudCoverCount << endl;
cout << "Pressure count: " << pressureCount << endl;

Results:

City location count: 32775 // supposed to be 14
Cloud cover count: -981987855 // supposed to be 81
Pressure count: 81 //correct

Sample of lines within files:

  • citylocation.txt:

    • [3, 3]-3-Big_City
    • [3, 7]-2-Mid_City
  • cloudcover.txt:

    • [0, 4]-70
    • [0, 5]-39
  • pressure.txt:

    • [2, 1]-42
    • [2, 2]-17

I've checked the regex and it does manage to match to the files I have, so I'm not too sure why this is happening. Could someone kindly enlighten me as to what I did wrong?

Aucun commentaire:

Enregistrer un commentaire