c++ifstream

Reading quoted string in c++


I am trying to read quoted string from a file and store it in string. I am reading string from the file and input file is like this:

"Rigatoni" starch 2.99
"Mac & Cheese" starch 0.50
"Potato Salad" starch 3.59
"Fudge Brownie" sweet 4.99
"Sugar Cookie" sweet 1.50

I have tried to do couple things:

1.

input.open(filename);
    if (input.fail())
    {
        std::cout << "File is not found!";
        exit(1);
    }   
    else
    {    
        std::string foodName = ""; std::string foodType = "";
        double cost;
        input >> foodName >> foodType >> cost;
        foodName = foodName.substr(1, foodName.size()-2);    
        std::cout << foodName << " " << foodType << " " << cost << std::endl;
    }


    input.close();

This version works only for the first line. After first line I am not getting whole quoted word. Another version reads whole quoted word, however, following word and number are separated.

input.open(filename);
        if (input.fail())
        {
            std::cout << "File is not found!";
            exit(1);
        }   
        else
        {


            std::string line = "";
            while (std::getline(input, line, '\n')) //delimeter is new line
            {
                if (line != "")
                {
                    std::stringstream stream(line);
                    std::string foodName = "";
                    while (std::getline(stream, foodName, '"') ) //delimeter is double quotes
                    {                   
                        std::cout << "current word " << foodName << std::endl;
                    }
                }
            }
        }   input.close();

My goal is to read 3 separate words. I looked over other similar topics in stackoverflow but could not find the right solution for my problem


Solution

  • I often use this to read between quotes:

    std::string skip; // throw this away
    std::string foodName;
    std::getline(std::getline(input, skip, '"'), foodName, '"');
    

    The first std::getline reads up to (and removes) the first quote. It returns the input stream so you can wrap that in another std::getline that reads in your variable up to the closing quote.

    For example like this:

    #include <string>
    #include <sstream>
    #include <iostream>
    
    std::istringstream input(R"~(
    "Rigatoni" starch 2.99
    "Mac & Cheese" starch 0.50
    "Potato Salad" starch 3.59
    "Fudge Brownie" sweet 4.99
    "Sugar Cookie" sweet 1.50
    )~");
    
    int main()
    {
        std::string skip; // dummy
        std::string foodName;
        std::string foodType;
        float foodValue;
    
        while(std::getline(std::getline(input, skip, '"'), foodName, '"') >> foodType >> foodValue)
        {
            std::cout << "food : " << foodName << '\n';
            std::cout << "type : " << foodType << '\n';
            std::cout << "value: " << foodValue << '\n';
            std::cout << '\n';
        }
    }
    

    Output:

    food : Rigatoni
    type : starch
    value: 2.99
    
    food : Mac & Cheese
    type : starch
    value: 0.5
    
    food : Potato Salad
    type : starch
    value: 3.59
    
    food : Fudge Brownie
    type : sweet
    value: 4.99
    
    food : Sugar Cookie
    type : sweet
    value: 1.5