Solved

How to get a file's timestamp from within C?

Posted on 2004-08-30
2
180 Views
Last Modified: 2010-04-15
I need to compare the timestamps of two files on disk from within a C program.  Can I do a system call, pipe the response from an 'ls' to a string, then parse the string (this is on a *nix system)?  Or is there an easier way?

Thanks
0
Comment
Question by:jimdgar
2 Comments
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 20 total points
ID: 11936819
I'd do a stat() to get the file info.

0
 
LVL 86

Accepted Solution

by:
jkr earned 80 total points
ID: 11937745
As grg99 wrote, 'stat()' is the way to go:

/* STAT.C: This program uses the _stat function to
 * report information about the file named STAT.C.
 */

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
   struct stat buf;
   int result;

   /* Get data associated with "stat.c": */
   result = stat( "stat.c", &buf );

   /* Check if statistics are valid: */
   if( result != 0 )
      perror( "Problem getting information" );
   else
   {
      /* Output some of the statistics: */
      printf( "File size     : %ld\n", buf.st_size );
      printf( "Time modified : %s", ctime( &buf.st_atime ) );
   }
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now