Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

WsprryPi/MonitorFile

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MonitorFile

License: MIT

MonitorFile is a lightweight C++ header-only library for monitoring file changes in the filesystem. It provides an easy-to-use API to detect modifications to a file based on its last write time.

🚀 Features

  • Minimal dependencies – Uses standard C++20 <filesystem>.
  • Exception-safe – Handles missing or deleted files gracefully.
  • Cross-platform – Works on Linux, macOS, and Windows (C++20 required).

📂 Repository Structure

MonitorFile/
│── src/                 # Source files
│   ├── monitorfile.hpp  # The header-only MonitorFile class
│   ├── main.cpp         # Test program for monitoring file changes
│   ├── Makefile         # Build system for testing
│── LICENSE.md           # MIT License
│── README.md            # Project documentation

🔧 Installation

To test the functionality, clone the repository and compile the test program:

git clone https://github.com/WsprryPi/MonitorFile.git
cd MonitorFile/src
make test

🛠 Usage

Include the Library

#include "monitorfile.hpp"

Basic Example

#include "monitorfile.hpp"
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>

int main()
{
    MonitorFile monitor;
    const std::string testFile = "testfile.txt";

    // Create a test file
    {
        std::ofstream file(testFile);
        file << "Initial content.\n";
    }

    // Start monitoring
    try
    {
        monitor.filemon(testFile);
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    std::cout << "Monitoring file: " << testFile << "\n";

    for (int i = 0; i < 5; ++i)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));

        // Modify the file
        if (i % 2 == 1)
        {
            std::ofstream file(testFile, std::ios::app);
            file << "Modification " << i << "\n";
        }

        try
        {
            if (monitor.changed())
            {
                std::cout << "File has been modified!\n";
            }
            else
            {
                std::cout << "No changes detected.\n";
            }
        }
        catch (const std::exception &e)
        {
            std::cerr << "Error: " << e.what() << std::endl;
            break;
        }
    }

    return 0;
}

🏗 Building & Testing

To compile and run the test program:

make test

To clean up compiled files:

make clean

To run static analysis (requires cppcheck):

make lint

🔒 Error Handling

MonitorFile throws std::runtime_error in these cases:

  • The file does not exist when calling filemon().
  • The file is deleted while monitoring.

To prevent crashes, always wrap calls to filemon() and changed() in a try-catch block.


📜 License

This project is licensed under the MIT License. See LICENSE.md for details.


🤝 Contributing

Pull requests and feature suggestions are welcome.

To contribute:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add new feature").
  4. Push to the branch (git push origin feature-name).
  5. Open a Pull Request.

📬 Contact

  • For issues, please open an Issue.
  • For questions, reach out to Lee C. Bussy (@LBussy).

About

A C++ class to monitor a file, and trigger an action upon change.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.