WereElf
WereElf

Reputation: 29

How to read string with spaces, until a '\r'?

I want to read a long string, and split it into smaller ones, where each new row of the big string is an entry for the smaller ones. However, I only want it to break at '\r', and not at spaces. This is a sample code of what I'm doing right now:

std::vector<std::string> m_list;
std::ifstream input("data.txt");
std::string str;
std::getline(input, str);
std::istringstream iss(str);
std::string temp;
while (iss >> temp)
{
    m_list.push_back(temp);
}

However, this code breaks the string upon encountering spaces, as well.

Upvotes: 1

Views: 140

Answers (1)

Gotiasits
Gotiasits

Reputation: 1175

You where definitely on the right path, you actually just need to skip the direct string stream operation. This should work fine:

    std::vector<std::string> m_list;
    std::ifstream input("data.txt");
    std::string str;        
    
    while (std::getline(input, str, '\r'))
    {
        m_list.push_back(str);
    }

Upvotes: 1

Related Questions