Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

776 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