Solved

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

Posted on 2001-07-12
5
747 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now