我有一个小问题,分割文本文件;在我的文本文件中有近1万个食谱,比如
-Ing_principal
是1成分
ingr 1
是2
是3
准备
现在我如何只能得到两个分隔符之间的成分:成分和准备。
所以我认为这个解决方案
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(); }
但是在无限循环中只返回ingr 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') {
我想应该用'p'代替'p':
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();
}
您希望访问由两个分隔符分隔的部分。那么直接的解决方案就是搜索这两个分隔符。然后,您可以复制中间内容以供以后使用。
我首先使用缓冲来自std::cin
的整个输入,因为它不支持在输入中任意移动。当使用文件时,这很可能是不必要的。
要执行搜索,最好的解决方案是<algorithm>
中的std::search
,您可以使用它来查找在另一个序列中出现的第一个序列。在您的情况下,这是在文件中找到"-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());
您可以使用它来查找两个分隔符,然后您可以使用在各自迭代器之间的部分来提取/打印/处理您感兴趣的文本。注意,您可能需要根据需要在分隔符中添加换行符。
我提供了一个小示例,尽管您可能希望将读取分解到某个函数中,或者返回各自的文本部分,或者使用一个函子来处理每个文本部分。
关于你的代码,有很多问题:
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')
最后,你的代码假设的东西和你告诉我们的不一样。(分隔符是"-I"后面跟着"not p"测试和"-P")