简体   繁体   English

在C ++中读取带引号的字符串

[英]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. 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. 我的目标是阅读3个单独的单词。 I looked over other similar topics in stackoverflow but could not find the right solution for my problem 我在stackoverflow中查看了其他类似的主题,但找不到适合我问题的正确解决方案

This becomes trivial with C++14's std::quoted : 这对于C ++ 14的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 . 是的,这将正确地将"some word"解析为some word Don't have a C++14 capable compiler? 没有具有C ++ 14功能的编译器? (You should, Fedora 22 ships with GCC 5.1.0 for example) then use Boost . (例如,Fedora 22随附GCC 5.1.0),然后使用Boost The standard library modelled std::quoted after this Boost component, so it should work just as fine. 标准库在此Boost组件之后std::quoted建模,因此它应该也可以正常工作。

There are some other problems in your code: 您的代码中还有其他一些问题:

  1. Streams have a conversion operator to bool. 流具有转换为bool的运算符。 This means you can do: if (!input.open(filename)) { ... } 这意味着您可以执行以下操作: if (!input.open(filename)) { ... }

  2. input.close() is redundant. input.close()是多余的。 The destructor will automatically close the stream. 析构函数将自动关闭流。

  3. You don't check if the input is valid, ie if (!(input >> a >> b >> c)) { // error } 您不检查输入是否有效,即if (!(input >> a >> b >> c)) { // error }

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. 第一个std :: getline读取(并删除)第一个引号。 It returns the input stream so you can wrap that in another std::getline that reads in your variable up to the closing quote. 它返回输入流,因此您可以将其包装在另一个std :: getline中 ,该变量读取变量直至结束引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM