簡體   English   中英

在C ++中讀取帶引號的字符串

[英]Reading quoted string in c++

我正在嘗試從文件中讀取帶引號的字符串並將其存儲在字符串中。 我正在從文件中讀取字符串,輸入文件是這樣的:

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

我試圖做幾件事:

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();

此版本僅適用於第一行。 第一行之后,我沒有得到完整的引號。 另一個版本讀取整個引用的單詞,但是,后面的單詞和數字是分開的。

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();

我的目標是閱讀3個單獨的單詞。 我在stackoverflow中查看了其他類似的主題,但找不到適合我問題的正確解決方案

這對於C ++ 14的std::quoted是微不足道的:

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

是的,這將正確地將"some word"解析為some word 沒有具有C ++ 14功能的編譯器? (例如,Fedora 22隨附GCC 5.1.0),然后使用Boost 標准庫在此Boost組件之后std::quoted建模,因此它應該也可以正常工作。

您的代碼中還有其他一些問題:

  1. 流具有轉換為bool的運算符。 這意味着您可以執行以下操作: if (!input.open(filename)) { ... }

  2. input.close()是多余的。 析構函數將自動關閉流。

  3. 您不檢查輸入是否有效,即if (!(input >> a >> b >> c)) { // error }

我經常用它來讀引號之間:

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

第一個std :: getline讀取(並刪除)第一個引號。 它返回輸入流,因此您可以將其包裝在另一個std :: getline中 ,該變量讀取變量直至結束引號。

例如這樣:

#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';
    }
}

輸出:

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