Solved

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

Posted on 2001-07-12
5
751 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 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