Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-07-12
5
Medium Priority
?
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

715 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