Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading quoted string in c++

Tags:

c++

ifstream

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

like image 424
Zafar Avatar asked Oct 12 '15 02:10

Zafar


2 Answers

This becomes trivial with C++14's std::quoted:

std::string foodName, foodType;
double cost;
while (fs >> std::quoted(foodName) >> foodType >> cost)
{
}

Yes, this will correctly parse "some word" into some word. Don't have a C++14 capable compiler? (You should, Fedora 22 ships with GCC 5.1.0 for example) then use Boost. The standard library modelled std::quoted after this Boost component, so it should work just as fine.

There are some other problems in your code:

  1. Streams have a conversion operator to bool. This means you can do: if (!input.open(filename)) { ... }

  2. input.close() is redundant. The destructor will automatically close the stream.

  3. You don't check if the input is valid, i.e. if (!(input >> a >> b >> c)) { // error }

like image 62
user5434961 Avatar answered Nov 12 '22 15:11

user5434961


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
like image 44
Galik Avatar answered Nov 12 '22 13:11

Galik