Comment detail

ファイル更新の監視 (Nested Flatten)
カバレッジ稼ぎ。ロジックはPython版とほとんど同じ。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdexcept>
#include <windows.h>

FILETIME mtime(const char* path)
{
    WIN32_FIND_DATA wfd;

    HANDLE h = ::FindFirstFile(path, &wfd);

    if (h == INVALID_HANDLE_VALUE)
    {
        throw std::runtime_error("file not found");
    }

    ::FindClose(h);

    return wfd.ftLastWriteTime;
}

int main()
{
    try
    {
        const char filename[] = "a.txt";

        FILETIME old_mtime = mtime(filename);

        while (true)
        {
            ::Sleep(100);

            const FILETIME new_mtime = mtime(filename);

            if (::CompareFileTime(&old_mtime, &new_mtime) != 0)
            {
                std::cout << "modified!" << std::endl;
            }

            old_mtime = new_mtime;
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

Index

Feed

Other

Link

Pathtraq

loading...