比较C ++中的2个文本文件

时间:2018-02-20 02:18:02

标签: c++

你好我试着比较2个文本文件并查找第一个文件中的字符串是否存在。

第一个文本文件:

11111     22222     33333     44444     55555     77777

第二个文本文件:

11111     22222     44444     AAAAA     BBBBB     55555     66666     CCCCC

输出应为:

 {
  "name": "[reference(resourceId(parameters('AutomationaccountRGName'), 'Microsoft.Automation/automationAccounts/configurations', parameters('AutomationaccountName'), 'swarmhost.localhost'))]",
  "type": "Microsoft.Automation/automationAccounts/configurations",
  "apiVersion": "[variables('automationApiVersion')]",
  "properties": {
    "logVerbose": "false",
    "description": "Configuration for worker hosts",
    "Source": {
      "type": "uri",
      "Value": "https://raw.githubusercontent.com/artisticcheese/SwarmARM/master/VMSS-Linked/swarmhost.ps1"
    }
  }
}

我得到的是:

11111 Match found
22222 Match found
33333 No Match
44444 Match found
55555 Match found
77777 No Match

这是我的代码:

11111 Match found
22222 Match found
33333 No Match
44444 No Match
55555 No Match
77777 No Match

1 个答案:

答案 0 :(得分:0)

让您的文件读取逻辑看起来像这样。您希望比较File2的全部内容,直到从File1中找到当前位置的匹配项。这应该做你想要的。

std::string lineA;
std::string lineB;
while (std::getline(File1, lineA))
{
    bool found(false);
    // read File2 until match is found
    while (std::getline(File2, lineB))
    {
        if (lineA == lineB)
        {
            found = true;
            File3 << lineA << " Match found" << std::endl;
            break;
        }
    }
    if (!found)
    {
        File3 << lineA << " No Match" << std::endl;
    }
    // clear the state of File2 stream
    File2.clear();
    File2.seekg(0, ios::beg);
}