C++ 如何解析c+中的引号和逗号+;

C++ 如何解析c+中的引号和逗号+;,c++,comma,scanf,quotation-marks,C++,Comma,Scanf,Quotation Marks,我有一个巨大的文件要解析。以前,它被空格或逗号分隔,我使用sscanf(字符串,“%lf%lf”、&aa和&bb)将数据输入我的程序 但现在数据格式更改为“122635.67039999”、“209705.75279999”,并带有逗号和引号。我不知道该怎么处理。事实上,我以前的代码是在网上找到的,我真的很难找到适合这种问题的文档。如果你能给我推荐一些,那就太好了。谢谢。如果字符串中有逗号分隔的数据,只需从字符串中删除“,如: 假设字符串是str1 str1.erase(std::remove(

我有一个巨大的文件要解析。以前,它被
空格
逗号
分隔,我使用
sscanf(字符串,“%lf%lf”、&aa和&bb)
将数据输入我的程序


但现在数据格式更改为
“122635.67039999”、“209705.75279999”
,并带有逗号和引号。我不知道该怎么处理。事实上,我以前的代码是在网上找到的,我真的很难找到适合这种问题的文档。如果你能给我推荐一些,那就太好了。谢谢。

如果字符串中有逗号分隔的数据,只需从字符串中删除
,如: 假设字符串是str1

str1.erase(std::remove(str1.begin(), str1.end(), '"'), str1.end());
这将删除所有出现的

//使用下面的代码将字符串转换为浮点
浮球f1;
std::stringstream-ss;
ssf1;

与其读取字符串,然后从字符串中删除逗号和引号,最后将数据转换为数字,我可能会创建一个区域设置对象,将逗号和引号分类为空白,在流中插入该区域设置,然后读取数字,而无需进一步告别

// here's our ctype facet:
class my_ctype : public std::ctype<char> {
public:
    mask const *get_table() { 
        static std::vector<std::ctype<char>::mask> 
            table(classic_table(), classic_table()+table_size);

        // tell it to classify quotes and commas as "space":
        table['"'] = (mask)space;
        table[','] = (mask)space;
        return &table[0];
    }
    my_ctype(size_t refs=0) : std::ctype<char>(get_table(), false, refs) { }
};
//这是我们的ctype方面:
类my_ctype:public std::ctype{
公众:
掩码常量*get_table(){
静态std::vector
表格(经典表格(),经典表格()+表格大小);
//告诉它将引号和逗号分类为“空格”:
表['”]=(掩码)空间;
表[',']=(掩码)空间;
返回&表[0];
}
my_ctype(size_t refs=0):std::ctype(get_table(),false,refs){
};
使用它,我们可以读取如下数据:

int main() { 
    // Test input from question:
    std::string input("\"122635.670399999\",\"209705.752799999\"");

    // Open the "file" of the input (from the string, for test purposes).
    std::istringstream infile(input);

    // Tell the stream to use the locale we defined above:
    infile.imbue(std::locale(std::locale(), new my_ctype));

    // Read the numbers into a vector of doubles:
    std:vector<double> numbers{std::istream_iterator<double>(infile),
                               std::istream_iterator<double>()};

    // Print out the sum of the numbers to show we read them:
    std::cout << std::accumulate(numbers.begin(), numbers.end(), 0.0);
}
int main(){
//来自问题的测试输入:
std::字符串输入(“122635.67039999\”,“209705.75279999\”);
//打开输入的“文件”(出于测试目的,从字符串中)。
std::istringstream填充(输入);
//告诉流使用我们在上面定义的区域设置:
imbue(std::locale(std::locale(),new my_ctype));
//将数字读入双精度向量:
std:向量数{std::istream_迭代器(infle),
std::istream_iterator()};
//打印出数字的总和,以显示我们读取的数字:

std::cout>value;
如果您愿意的话。您可以按照通常读取由空格分隔的数字的任何方式读取数字——因为就流而言,这正是您所拥有的。

它是否由数据行组成?如果是,为什么不读取一行,然后在逗号处解析该行,然后删除quotes?是的,数据是成行的。你能解释一下如何删除引号吗?还有谷歌的“如何解析CSV?”“,即使如此,您也应该找到足够的信息。您需要验证文件是否包含引号和逗号,还是只想读取数字,而不管是否存在逗号和/或引号?我只需要读取数字,而且我认为整体格式是统一的。”(与我为每行列出的完全相同,只有数百万行)。您也可以这样做,或者如果您在字符串中获得类似
“122635.67039999”
的数据,则删除
"
并将该字符串转换为float。两种方法都可以解决您的问题。谢谢,但在我的应用程序中,
&aa和&bb
是两个不同的变量。现在所有这些变量都将导入到
aa
中。我仍在阅读
stringstream
的说明。这很有效!除了解决我的问题,您的回复还向我介绍了很多对于新的东西。顺便说一句,你是否应该在你的代码中将
s.imbue
更改为
infle.imbue
int main() { 
    // Test input from question:
    std::string input("\"122635.670399999\",\"209705.752799999\"");

    // Open the "file" of the input (from the string, for test purposes).
    std::istringstream infile(input);

    // Tell the stream to use the locale we defined above:
    infile.imbue(std::locale(std::locale(), new my_ctype));

    // Read the numbers into a vector of doubles:
    std:vector<double> numbers{std::istream_iterator<double>(infile),
                               std::istream_iterator<double>()};

    // Print out the sum of the numbers to show we read them:
    std::cout << std::accumulate(numbers.begin(), numbers.end(), 0.0);
}