samedi 24 juillet 2021

How to choose design pattern for processing of input data?

I have a code snippet that is used to process strings of text files. The function processFile function is called for every file in the directory

void processFile(const std::string& sourceFileName) {
    std::ifstream sourceFile;
    sourceFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    try {
        sourceFile.open(sourceFileName);
        if (sourceFile.is_open()) {
            while (sourceFile.peek() != EOF) {
                std::string line;
                std::getline(sourceFile,line);
                // process line here
            }
            sourceFile.close();
        }
    }
    catch (const std::ifstream::failure& exception) {
        std::cout << "some exception text here" << std::endl;
        return;
    }
    catch (...) {
        std::cout << "some another exception text here" << std::endl;
    }
}

Besides files, I also need to read data from pipes and from sockets. What design pattern can you use to abstractly read an entity? Could you please give me some examples?

Aucun commentaire:

Enregistrer un commentaire