首页 > 解决方案 > 使用 c++ 连续检查文件中的任何更改?

问题描述

我必须检查使用 c++ 语言在文本文件中所做的任何更改。

标签: c++text

解决方案


#include<stdio.h>
#include<iostream>
#include <fstream>
#include <string>
#include<stdlib.h>
#include <windows.h>
using namespace std;
string *fileContent = NULL;
int main()
{

    //init app
    do
    {
        ifstream inFile("C:\\Users\\ur185010\\Desktop\\uuid.txt");
        string *content = new string((std::istreambuf_iterator<char>(inFile)),
            (std::istreambuf_iterator<char>()));
        //cout << content->c_str();
        bool dataChanged = false;
        {
        if ((fileContent == NULL) || (strcmp(fileContent->c_str(), content->c_str()) != 0))
        {
            cout << "updated\n";
            if (fileContent) {
                delete fileContent;
            }
            fileContent = new string(content->c_str());
            dataChanged = true;
        }
        }
        if (dataChanged) {
            printf("Data Changed \n", fileContent->c_str());
            // call api to parse and update data base.
        }
        Sleep(5000);

    } while (1);
    getchar();
    return 0;
    //inFile.open("C:\\Users\\ur185010\\Desktop\\uuid.txt");

}

推荐阅读