Solved

How to get the time/date of a file in unix C

Posted on 2001-07-12
5
748 Views
Last Modified: 2013-12-05
In VC, you can use _stat(filename, buf)  to get the st_mtime of a file.

What does one use in classical unix C?

(and the name of the required .H file)

(this is on sco svr5 unix)
0
Comment
Question by:nrbreen
  • 3
5 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6279755
You can use stat() function.

The headers should be the same as the VC headers.
<sys/types.h>
<sys/stat.h>

This is a non ANSI C++ function that most Unix compilers support.
0
 
LVL 1

Expert Comment

by:frayday
ID: 6281566
That should do it !

from the man page ...

#include <sys/stat.h>
#include <unistd.h>

int stat(const char *file_name, struct stat *buf);

where structure stat looks like ...

struct stat
{
dev_t         st_dev;      /* device */
ino_t         st_ino;      /* inode */
mode_t        st_mode;     /* protection */
nlink_t       st_nlink;    /* number of hard links */
uid_t         st_uid;      /* user ID of owner */
gid_t         st_gid;      /* group ID of owner */
dev_t         st_rdev;     /* device type (if inode device) */
off_t         st_size;     /* total size, in bytes */
 unsigned long st_blksize;  /* blocksize for filesystem I/O */
unsigned long st_blocks;   /* number of blocks allocated */

//these seem to be what you are looking for:
time_t        st_atime;    /* time of last access */time_t        st_mtime;    /* time of last modification */time_t        st_ctime;    /* time of last change */
};


0
 
LVL 30

Expert Comment

by:Axter
ID: 6281591
frayday,
What compiler are you using, and on what type of Unix system?

My man page shows <sys/types.h> and <sys/stat.h>
I've checked it out on an HP and Dec servers.

The unistd.h header file should not be needed.

nrbreen,
Who makes SCO svr5 unix?
And what compiler are you using on the unix?
0
 
LVL 3

Author Comment

by:nrbreen
ID: 6284279
Ah well - it was as simple as the underscore on the front of the "struct _stat"
OK - that works, thanks.

(SCO make UnixWare - I think it is just for Intel CPUs)

nb
0
 
LVL 30

Expert Comment

by:Axter
ID: 6284327
FYI,
It should also work in windows with out the underscore.
It should be able to compile both ways in windows.
Example:
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
     struct stat statdata;
     struct _stat statdata2;
     stat("test.c",&statdata);
     _stat("test.c",&statdata2);
     return 0;
}

In the above code, both functions will compile in VC 6.0.
VC prefixes an underscore "_" to all lowercase non-ANSI functions.
_atoi64
_itoa
_getch
_ungetch

But you don't have to use the underscore in your code.  All of them will compile with or without the underscore.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SNMP error No Such Object available on this agent at this OID 3 200
Sed question 2 69
Why isn't object file created? 6 43
object oriented programming comparison 5 57
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

27 Experts available now in Live!

Get 1:1 Help Now