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

时间:2014-01-01 14:01:22

标签: c++ string

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

文本文件如下所示:

----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--

我想搜索字段名称'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'
            }
        }
    }
}

如何更正我的代码以搜索Content-Type(text / html或text / plain)并返回边界标记之间的所有后续行?

1 个答案:

答案 0 :(得分:1)

#include <string>
#include <iostream>
// ...
std::string keyword;
std::string word;

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