Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make data from a text file be filtered into a vector

I have a little program I don’t know how to make. Basically, it is a function to create a vector with data from a text file that meets a parameter in its text.

text_in_vector("file.txt", "10")

text example:

Karen10, Lili12, Stacy13, Mack10

vector results

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"Karen10","Mack10"}

>Solution :

Try something like this:

#include <fstream>
#include <vector>
#include <string>
#include <iomanip>

std::vector<std::string> text_in_vector(const std::string &fileName, const std::string &searchStr)
{
    std::vector<std::string> vec;
    std::ifstream inFile(fileName);
    std::string str;
    while (std::getline(inFile >> std::ws, str, ','))
    {
        if (str.find(searchStr) != std::string::npos)
            vec.push_back(str);
    }
    return vec;
}

Online Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading