我刚刚开始使用c ++,并且很想知道如何计算文件中特定点之间的界限。我已经成功地找到了我想要开始计算的第一点,但我不知道如何进一步计算。
这是我到目前为止所得到的:
ifstream fin;
string line;
char* search = "Key:";
char* search2 = "Color:";
// Open the file.
fin.open(filename);
// Check if it was successful in opening the file.
if (fin.fail() == true)
{
return false;
}
// Read from the file and continue to read until the end of the file is reached.
while (getline(fin, line)) {
if (line.find(search, 0) != string::npos) {
//Start counting line ??
}
}
感谢任何帮助。
答案 0 :(得分:2)
使用您的代码,例如:
unsigned int count_lines=0;
unsigned int ignore_lines=0;
// Read from the file and continue to read until the end of the file is reached.
while (getline(fin, line)) {
count_lines++;
if (line.find(search, 0) != string::npos) {
ignore_lines = count_lines;
}
if (line.find(search2, 0) != string::npos) {
// The number of rows is count_lines - ignore_lines
}
}
答案 1 :(得分:0)
首先你需要一个变量来存储计数,然后你还需要一个变量来确定你当前是否在计算:
int count = 0;
bool counting = false;
while(...) {
if(...)
counting = true;
if(...)
counting = false;
if(counting)
++count
}