在C++中解析用引号括起来的东西

Parsing out things surrounded by quotes in C++

本文关键字:起来 C++      更新时间:2023-10-16

我正在尝试制作一个解析器,它只接受用引号括起来的文本,并将其放置在一个新文件中。我已经尝试过很多次了,但都不知道它必须将原始文本从文件中取出,然后将其放置到一个新的文件中。


这就是我目前拥有的:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char current_letter;
    char quote_mark = '"';
    int isquote = 0;
    std::cin >> current_letter;
    LOOP : do
{
    if(current_letter == quote_mark) {++isquote;}
        if(isquote == 1 && current_letter != quote_mark) {std::cout << current_letter;}
        if(isquote == 1 && current_letter == quote_mark) {--isquote;}
        if(isquote == 0) {goto LOOP;}
} while (cin >> current_letter);
if(cin != current_letter) {cout << "END" <<endl;}
return(0);

它现在不打印任何东西,但它过去只打印随机的东西或只是引号。

您可以这样做:

string str;
getline(cin, str);
string currStr = "";
for (int i = 0; i < str.length(); i++)
{
  if (str[i] != '"') currStr += str[i];
}
cout << currStr << "n";

但是,如果要解析的文本中有双引号,这将不起作用

相关文章: