【问题标题】:Parsing complex text file using C++使用 C++ 解析复杂的文本文件
【发布时间】:2020-01-11 05:55:36
【问题描述】:

我的任务是使用 C++ 代码解析这个文本文件。 我将不得不阅读此文件并查找关键字“结果摘要:”并在结果摘要之后收集全部内容:在一个名为字符串数据的变量中;并将数据内容写入csv文件。 我需要帮助将内容转换为可变字符串数据...

我的文本文件看起来像:

文本文件.txt: 报告文件:C:\Program Files (x86)\Enterprise\Reports\Export_20190617099828.txt

Time log:
---------------------------------------

Time:   9:03:49 AM
Export of archive Walton, Amelia was completed successfully.

End time:   9:03:49 AM


Results Summary:
---------------------------------------

Juma, Jmelia : Processed
Storage server: SRE!
Export = 12; Failed = 0; Remaining = 0 (0%)


Number archives processed:      1
Number archives being processed:    0
Number archives part processed:     0
Number archives not processed:      0
Number archives failed:         0

---------------------------------------------------------------

我想解析这个文本文件并将解析后的数据写入csv文件。

要解析的数据是:

Results Summary:
---------------------------------------

Juma, Jmelia : Processed
Storage server: SRE!
Export = 12; Failed = 0; Remaining = 0 (0%)

我尝试了各种方法,但都没有成功。

string search = "Results Summary:"
getline(inFile, line); // get line from file
pos = line.find(search); // search
if (pos == string::npos) // string::npos is returned if string is not found
{
    WriteLogFile("Found Results Summary:!\n");

    break;
} else {
    WriteLogFile("Not Found string searched for!\n");
}

//输出:

下面的输出必须写在名为 test.csv 的 .csv 文件中

Juma, Jmelia 
Storage server: SRE!
Export = 12; 
Failed = 0;
Remaining = 0 (0%)

【问题讨论】:

  • 预期输出为 test.csv
  • 为您的输入提供示例测试输出。
  • Juma, Jmelia 存储服务器:SRE!出口 = 12;失败 = 0; Remaining = 0 (0%) 必须打印在 csv 文件中
  • 可以用正则表达式解决。

标签: c++ parsing visual-studio-2010 text


【解决方案1】:

我会这样做:

#include <iostream>

using namespace std;

int main()
{
             ifstream input;
        size_t pos;
              string line;
        int found_string=0;
        input.open("t.txt");
        if(input.is_open())
        {
            while(getline(input,line))
            {
                pos = line.find("Results Summary:");
                if(pos!=string::npos) // string::npos is returned if string is not found
                {
                    found_string=1;
                    cout <<"Found!";
                }


                if(found_string==1)
                {
                    cout<<line<<endl;
                   //do whatever to save it in the csv
                }

            }
        }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2018-11-10
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多