使用2个字符串分隔符c ++选择文本文件的一部分

时间:2015-08-08 10:12:01

标签: c++ string split getline

我对分割文本文件有点问题; 在我的文本文件中有近1万个收件人,如

-Ing_principal

ingr 1

-Ingredients

ingr 1

ingr 2

ingr 3

- 制备

现在我如何才能获得作为成分和准备的2个分隔符之间的成分。

所以我认为这个解决方案

int main() {
string s, t;
bool i = false;
ifstream ricette;
ofstream ingredienti;
ingredienti.open("ingredienti.txt");
ricette.open("ricette.txt", ios::out);
while(ricette) {        
    getline (ricette, s);
    if (s[0] == '-' && s[1] == 'I' && s[5] != 'P') {
        i = true;
        getline(ricette, t);
            while (i) {
                if (t[0] != '-' && t[1] != 'P')
                    cout <<  t << endl;
                else i = false; 

        }
    }
}
ingredienti.close();
ingredienti.close();  }

但这只在无限循环中返回1。 任何人都有很好的解决方案或建议吗?

2 个答案:

答案 0 :(得分:1)

似乎你在这个循环中没有读取新的输入行:

        while (i) {
            if (t[0] != '-' && t[1] != 'P')
                cout <<  t << endl;
            else i = false; 

            // Here you'll need to read the next line
        }

这条线看起来也很奇怪:

if (s[0] == '-' && s[1] == 'I' && s[5] != 'P') {

我想这应该是一个&#39; p&#39;而不是&#39; P&#39;:

if (s[0] == '-' && s[1] == 'I' && s[5] != 'p') {

BTW - 你关闭同一个文件两次:

ingredienti.close();
ingredienti.close();

但是,我会使用另一种方法来避免两个while语句。类似的东西:

int main() {
    string s;
    bool foundInterestingSection = false;
    ifstream ricette("ricette.txt");
    ofstream ingredienti("ingredienti.txt");

    while(getline (ricette, s))
    {
        if (foundInterestingSection)
        {
            if (s == "-Preparation")
            {
                // The interesting section ends now
                foundInterestingSection = false;
            }
            else
            {
                cout <<  s << endl;

                // Write to output file
                ingredienti << s << endl;
            }
        }
        else
        {
            if (s == "-Ingredients")
            {
                // The interesting section starts now
                foundInterestingSection = true;
            }
        }
    }
    ingredienti.close();
    ricette.close();
}

答案 1 :(得分:0)

您想要访问由两个分隔符分隔的部分。然后直接的解决方案是搜索这两个分隔符。然后,您可以复制中间内容以供进一步使用。

我使用的方法首先缓冲来自std::cin的整个输入,因为它不支持在输入中任意移动。使用文件时,这很可能不是必需的。

要执行搜索,最佳解决方案是来自<algorithm>的{​​{3}},您可以使用它来查找另一个序列中第一次出现的序列。在您的情况下,这是在文件中找到"-Ingredients""-Preparation"

std::string const start_delimiter{"-Ingredients"};
auto start = std::search(from, to, start_delimiter.begin(), start_delimiter.end());
// start now points to '-', assuming the string was found
std::advance(start, delimiter.size());
// start now points delimiter.size() characters AFTER the '-', which
// is the character following the delimiter string
// ...
std::string const end_delimiter{"-Preparation"};
auto end = std::search(start, to, end_delimiter.begin(), end_delimiter.end());
// Your text is between [start,end)
from = end;
std::advance(from, end_delimiter.size());

使用它来查找两个分隔符,然后您可以使用各个迭代器之间的部分来提取/打印/使用您感兴趣的文本。请注意,您可能需要添加换行符你需要的分隔符。

我把std::search放在一起,虽然你可能想把读数归结为某个函数,要么返回相应的文本部分,要么使用仿函数来处理每个文本部分。

关于您的代码,存在多个问题:

ifstream ricette;
// ...
ricette.open("ricette.txt", ios::out);
// ...
getline(ricette, t);

您获取输入文件流,打开输出,然后读取吗?

  getline(ricette, t);
  while (i) {
            // ...
  }

你只读了一行成分。您需要在循环内部执行读取,否则t将永远不会在while循环内部发生变化(这就是您获得无限循环的原因)。

ingredienti.close();
ingredienti.close();

......双关......

然后,通常,您应该直接测试输入操作,即getline

std::string t; // Use better names, define variables near their use
while(getline(ricette, t)) {
  if (t[0] == '-' && t[1] == 'P') {
   break;
  }
}
// could be eof/failure OR "-P.." found

然后,看看你的测试,想想输入空行时会发生什么?或者只有一个字符的行?您也需要测试尺寸:

if (t.size() > 1 && t[0] == '-' && t[1] == 'P')

最后,您的代码假设与您告诉我们的内容不同。 (你的分隔符是&#34; -I&#34;其次是&#34;不是p&#34;测试以及&#34; -P&#34;)