C++ 在文本文件中搜索字段名并将所有后续行返回控制台-C++;

C++ 在文本文件中搜索字段名并将所有后续行返回控制台-C++;,c++,string,C++,String,我正在尝试创建一个函数,该函数将在文本文件中扫描特定字段名,并将所有后续文本行返回到控制台 文本文件如下所示: ----8896484051606557286 Content-Type: text/html; Content-Transfer-Encoding: 7Bit <html> <body bgcolor="#ffffff"> <div style="border-color: #00FFFF; border-right-width: 0px; borde

我正在尝试创建一个函数,该函数将在文本文件中扫描特定字段名,并将所有后续文本行返回到控制台

文本文件如下所示:

----8896484051606557286
Content-Type: text/html;
Content-Transfer-Encoding: 7Bit

<html>
<body bgcolor="#ffffff">
<div style="border-color: #00FFFF; border-right-width: 0px; border-bottom-   width: 0px; margin-bottom: 0px;" align="center">
<table style="border: 1px; border-style: solid; border-color:#000000;"  cellpadding="5" cellspacing="0" bgcolor="#CCFFAA">
<tr>
<td style="border: 0px; border-bottom: 1px; border-style: solid; border-color:#000000;">
<center>

----8896484051606557286--
——8896484051606557286
内容类型:text/html;
内容传输编码:7Bit
----8896484051606557286--
我想搜索字段名“Content-Type:text/html或Content-Type:text/plain”,并返回边界标记(--8896484051606557286--)之间紧跟其后的文本

以下是我到目前为止的想法:

int main(int argc, char *argv[])
{
    string path = "C:/TEST/";
    string common = "SPAM";

    for (int count = 1; count <= 100; ++count) {
        //Convert count to an string.
        stringstream ss;
        ss << count;
        string numstring = ss.str();
        string filename = path + common + numstring + ".txt.";

        ifstream infile(filename.c_str());
        string line;
        string CT1 = "Content-Type: text/html";
        string CT2 = "Content-Type: text/plain";

        while (!infile.eof()) {
            getline(infile, line);
            int index = fileStr.find("Content-Type:", 0)

            if (index >= 0) {
                index += 13;
            } //(just add the size of Content - Type:)
            int endIndex = fileString.find(";", index)
                           string contentType = fileString.subString(index, endIndex index);
            if (contentType.compare("text/html") == 0) {
                // then parse as text / html //
            } else if (contentType.compare("text/plain") == 0) {
                 //parse as the text/html'
            }
        }
    }
}
intmain(intargc,char*argv[])
{
字符串path=“C:/TEST/”;
string common=“垃圾邮件”;
对于(int count=1;count
#包括
#包括
// ...
std::字符串关键字;
字符串字;
getline(文件,关键字);
做
{
std::cin>>单词;
}
while(关键字.find(word)==std::string::npos);

谢谢分享您的编码尝试,但您到底需要什么帮助?这里没有实际问题,您的问题是?-我猜您不是在问“有人可以在//中编写代码,然后解析为文本/html吗”,因为这不是“编写代码服务”,但这是问答式网站。抱歉-这是我第一次使用此网站。我的问题是如何更正代码以搜索内容类型(text/html或text/plain)然后返回边界标记之间的所有后续行?ThanksI不想使用cin,因为我需要扫描一个包含70000多个文件的目录。我需要函数来读取目录中的每个文件,并查找“Content Type:text/html”或“Content Type:text/plain”并返回边界标记之间的所有文本,忽略文件中的所有其他文本。该程序最终将标记边界标记之间的所有字符串数据,并将它们存储在向量/数组中,以便与SQlite数据库形成连接。我只是在第一个障碍上挣扎。thanks@TheBlueNile
find。-键入f-exec yourapp'{}“< /COD> > ErCurthe-你能把这个解释稍微扩展一下吗?因为我在编码C++方面有点新手,也许把它放到我的代码的上下文中……很多。thanks@TheBlueNile您不必将目录列表编码到应用程序中,只需编写代码来处理一个文件,然后使用外部工具传递文件作为应用程序的参数。@Erburat谢谢你的建议。我仍在努力编写代码来处理一个文件。。。
#include <string>
#include <iostream>
// ...
std::string keyword;
std::string word;

getline(file, keyword);
do
{
    std::cin >> word;
}
while (keyword.find(word) == std::string::npos);