C++:查找一本书的章节

C++: Finding the chapter of a book

本文关键字:一本 查找 C++      更新时间:2023-10-16

我有一个相当大的文本文件,希望有一个C++程序扫描它并返回我想要的章节。例如,假设该文件名为"MyTales.txt",其内容为

MYTALE by Me (classifying what book it is)
CHAPTER ONE
Part 1: The Start

其中Part 1(整体)是要返回的节。

作为一个基本模板,到目前为止我想到的是:

#include<iostream>
#include<fstream>
ifstream in;
ofstream out;
string book, chapter, part;
int main() {
 cout << "Please enter the part to locate:nBook: ";
 cin >> book;
 cout << "Chapter: ";
 cin >> chapter;
 cout << "Part: ";
 cin >> part;
 in.open("MyTales.txt");
 while (in >> data) {
  // where data is assigned
 }
 out.open("BookParts.txt");
 out << data;
 in.close();
 out.close();
}

我只是不知道如何正确分配"数据"。我修改了一些字符串函数和其他东西,但似乎什么都没用。非常感谢您的帮助!

第一个问题是根据其数值生成章节的标题。获得名称后,忽略包括章节名称在内的逐行输入。然后阅读输入,直到你遇到一行以"Chap章"开头或输入的末尾。最后输出你读到的所有内容。