number of lines in c++ files

#include <iostream>
#include <fstream>
#include <string>

int countLines(const std::string& filename);

int main() {
    const std::string filename = "example.cpp";
    int lines = countLines(filename);

    std::cout << "Number of lines in " << filename << ": " << lines << std::endl;

    return 0;
}

int countLines(const std::string& filename) {
    std::ifstream file(filename);

    if (!file.is_open()) {
        std::cerr << "Error opening file: " << filename << std::endl;
        return -1;
    }

    int lineCount = 0;
    std::string line;

    while (std::getline(file, line)) {
        if (!line.empty() && line.find_first_not_of(' ') != std::string::npos) {
            lineCount++;
        }
    }

    file.close();
    return lineCount;
}