-2

I have the following code:

struct stat info;
stat("/Users/john/test.txt", &info);
printf("%s\n", ctime(&info.st_mtimespec));

in which I am trying to get the last modified time of the file as displayed in the ls -l command in the format:

Jan 29 19:39

Unfortunately, above code doesn't work.I get the following error message on xcode:

Conflicting types for ctime

How can I fix it ? If there any alternative approaches to get the time in format mentioned, please do mention.

Jenna Maiz
  • 792
  • 4
  • 17
  • 38
  • It "doesn't work" because? – kaylum Mar 25 '16 at 04:16
  • I get a "conflicting type" error message for the ctime function – Jenna Maiz Mar 25 '16 at 04:31
  • Don't you think it would make sense to mention that in the question? Please edit your question to put in the exact error message. – kaylum Mar 25 '16 at 04:32
  • Jenna, you'd be surprised at how often the error message provides some hint as to what problem is occurring. – ghoti Mar 25 '16 at 04:32
  • @ghoti I understand the problem-ctime is not getting the arguments it needs. What I am having trouble with is what to plug-in to the thing.... – Jenna Maiz Mar 25 '16 at 04:46
  • "Questions seeking debugging help ("why isn't this code working?") must include the **desired behavior**, **a specific problem or error and the shortest code necessary** to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve)" – Antti Haapala -- Слава Україні Mar 25 '16 at 04:48
  • Possible duplicate: [Getting the last modified date of a file in C](http://stackoverflow.com/questions/11373505/getting-the-last-modified-date-of-a-file-in-c) – kaylum Mar 25 '16 at 04:51
  • @AnttiHaapala There is a "desired behavior": I want the time format expressed as given in the example : Jan 29 19:39.... – Jenna Maiz Mar 25 '16 at 04:53
  • You need to edit your question to include the **complete** output from the compiler, because you've omitted some of it. Copy and paste it. Do not retype it. – rob mayoff Mar 25 '16 at 04:57
  • When @AnttiHaapala comment was made all you had was "it doesn't work". Which is not a *specific* problem or error. – kaylum Mar 25 '16 at 04:57
  • @robmayoff I am on xcode - when I type above, I get the red exclamation with the above message. I will paste the entire code, if it helps( which is just the main declaration)... – Jenna Maiz Mar 25 '16 at 05:03
  • Open the Issue navigator (View > Navigators > Show Issue Navigator; default shortcut: ⌘4). Open all of the disclosure triangles to show all of the compiler messages. Select all of the messages (click one message and choose Edit > Select All). Choose Edit > Copy to copy them to the pasteboard. Edit your question here on stackoverflow and paste in the messages. – rob mayoff Mar 25 '16 at 05:06
  • when calling the function `stat()`, always check the returned value, because there are many things that can cause the `stat()` function to fail, – user3629249 Mar 25 '16 at 05:20

3 Answers3

0

Checking on struct stat's declaration you'll see that st_mtimespec must be st_mtime.

Then, basing on this question, I rearranged your code like this:

struct stat info;
struct tm* tm_info;
char buffer[16];

stat("/Users/john/test.txt", &info);
tm_info = localtime(&info.st_mtime);
strftime(buffer, sizeof(buffer), "%b %d %H:%M", tm_info);

printf("%s\n", buffer);

Hope it helps.

Community
  • 1
  • 1
dubafek
  • 1,073
  • 9
  • 21
0

I believe this is what you are looking for:

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

int main(int argc, char *argv[])
{
    struct stat info;
    stat("sample.txt", &info);
    printf("%.12s\n", 4+ctime(&info.st_mtimespec));
    return 0;
}

Output( same as time field of ls -l):

Feb  4 00:43

(This was for a random file on my computer).

Jenna Maiz
  • 792
  • 4
  • 17
  • 38
0

does your code have:

#include <time.h>

also, the ctime() function expects the passed parameter to be a POINTER to a time_t.

Here is the struct pointed to by the stat() function:

          struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           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 ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for filesystem I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
           time_t    st_atime;   /* time of last access */
           time_t    st_mtime;   /* time of last modification */
           time_t    st_ctime;   /* time of last status change */
       };

Notice that none of the fields are a st_mtimespec

perhaps you meant st_mtime

Note: your running OS-X and I'm running linux, but the OS-X should have the very same definitions of field names.

user3629249
  • 16,402
  • 1
  • 16
  • 17