在C ++中读取带引号的字符串 [英] Reading quoted string in c++

查看:683
本文介绍了在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

我试图做几件事:

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

我的目标是阅读3个单独的单词.我查看了stackoverflow中的其他类似主题,但找不到适合我问题的正确解决方案

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

推荐答案

我经常使用它来读取引号之间的内容:

I often use this to read between quotes:

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

第一个 std :: getline 读取到(并删除)的第一引号.它返回输入流,因此您可以将其包装在另一个 std :: getline 会读取变量,直到右引号为止.

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.

例如这样的

#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

这篇关于在C ++中读取带引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆