Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:54:34+00:00 2026-05-28T00:54:34+00:00

I have a program that wants to check if a file has been modified.

  • 0

I have a program that wants to check if a file has been modified. The regtest fails, and, indeed, despite the file having changed, the st_mtime has not! Moreover, an external stat confirms the same.

I believe st_mtime should change because stat(2) says

The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes).

Here’s a bit of C code that illustrates the matter:

#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


void touch(const char *fn, const char *contents)
{
    FILE *fp;
    assert(fp = fopen(fn, "w"));
    fprintf(fp, contents);
    fclose(fp);
}


int main( int argc, char *argv[] )
{
    struct stat st;
    char path[] = "/tmp/foo";
    time_t m1, m2;
    unsigned int t;

    touch(path, "hello\n");
    assert(!stat(path, &st));
    m1 = st.st_mtime;

    touch(path, "hello, world!\n");
    t = sleep(2);
    assert(!stat(path, &st));
    m2 = st.st_mtime;

    printf("Sleep remaining: %lu\n", t);
    printf("Elapsed modtime=%lu\n", m2 - m1);
}

Here’s something to offer to bash to confirm that it’s not just caching within the C program:

$ while true; do stat /tmp/foo | grep Modify; sleep 1;done

Any suggestions what is happening? Fwiw, this is running on a system thus identified:

jeff@london:src $ uname -a
Linux london 2.6.32-37-generic #81-Ubuntu SMP Fri Dec 2 20:32:42 UTC 2011 x86_64 GNU/Linux
jeff@london:src $ 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:54:34+00:00Added an answer on May 28, 2026 at 12:54 am

    First off, I’d strongly encourage you to not use statements with side effects within asserts. I understand it’s convenient and if the code is never going to go production it doesn’t really matter, but it’s not good form. In particular, if you ever define NDEBUG, the asserts get converted to a noop, the code for the condition is never executed.

    Now then, the problem is most likely that you put the sleep call after both touch calls, instead of between them. Change this:

    touch(path, "hello\n");
    assert(!stat(path, &st));
    m1 = st.st_mtime;
    
    touch(path, "hello, world!\n");
    t = sleep(2);
    assert(!stat(path, &st));
    m2 = st.st_mtime;
    

    to

    touch(path, "hello\n");
    int rv = stat(path, &st);
    assert(!rv);
    m1 = st.st_mtime;
    
    t = sleep(2);
    
    touch(path, "hello, world!\n");
    int rv = stat(path, &st);
    assert(!rv);
    m2 = st.st_mtime;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a program that reads metadata from an MP3 file. My
I have a python program/file that I want to run repeatedly and calculate the
I have a program that is been implemented in C++ which I now want
I have a vb.net 2.0 program, that has a batch/queue routine to execute tasks
I have a program that takes a list of names from a file and
Hey everyone. I have an exe file that runs a console program that was
I am having a problem in the program code as I have changed my
I have a program that generates a plain text file. The structure (layout) is
I have an old ClickOnce program, which has been replaced by a new one
I have a program that I want to use as an autorun. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.