Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Displaying File Date and TimevStamp

Posted on 2002-06-24
6
Medium Priority
?
443 Views
Last Modified: 2010-04-15
I asked this question earlier, But I am not getting the correct results.
Tyring to run this code on an AIX /Unix system.
But I am not able to display the Date and time of the File correctly. What am I doing wrong.? Can anyone help me

 DIR *dirp;
    struct dirent *fileNames[2048];  
    char dtBuf[30];
    struct dirent *direntp;
    struct stat info;
   
    dirp = opendir( InboxDirectory );
    if( dirp != NULL ) {
     for(;;) {
       direntp = readdir( dirp);
     if( direntp == NULL ) break;
      if (directp->d_name[0] == '.')
        continue;    
     fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
     memset(fileNames[fcnt], '\0', sizeof(struct dirent));
     memset(&info, '\0', sizeof(struct stat));
     memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
     stat (fileNames[fcnt]->d_name, &info);
     strftime(dtBuf, 30, "%D, %H:%M:%S", localtime(&info.st_mtime));
     printf("File Date/Time Stamp: %s", dtBuf);    
     }
    closedir( dirp );
 }
0
Comment
Question by:Loysi
[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
  • 4
  • 2
6 Comments
 

Author Comment

by:Loysi
ID: 7103976
couple of errors in the code. I just fixed. (Portion of the code I am using.)

DIR *dirp;
   struct dirent *fileNames[2048];  
   char dtBuf[30];
   struct dirent *direntp;
   struct stat info;
   
   dirp = opendir( InboxDirectory );
   if( dirp != NULL ) {
    for(;;) {
      direntp = readdir( dirp);
     if( direntp == NULL ) break;
      if (directp->d_name[0] == '.')
        continue;    
     fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
     memset(fileNames[fcnt], '\0', sizeof(struct dirent));
     memset(&info, '\0', sizeof(struct stat));
     memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
     stat (fileNames[fcnt]->d_name, &info);
     strftime(dtBuf, 30, "%D, %H:%M:%S", localtime(&info.st_mtime));
     printf("File Date/Time Stamp: %s\n", dtBuf);  
     fcnt++;
    }
   closedir( dirp );
}
0
 

Expert Comment

by:woregul
ID: 7104235
I ran your snippet on AIX, and it produced good output, so I am not sure I understand your problem. See if the following code will resolve your issue. I modified the date format a little.


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
  int fcnt = 0;
  DIR *dirp;
  struct dirent *fileNames[2048];
  char dtBuf[30];
  struct dirent *direntp;
  struct stat info;

  dirp = opendir( "." );

  if( dirp != NULL ) {
   for(;;) {
     direntp = readdir( dirp);
    if( direntp == NULL ) break;
     if (direntp->d_name[0] == '.')
       continue;

    fileNames[fcnt] = (struct dirent *) malloc(sizeof(struct dirent));
    memset(fileNames[fcnt], '\0', sizeof(struct dirent));
    memset(&info, '\0', sizeof(struct stat));
    memcpy(fileNames[fcnt], direntp, sizeof(struct dirent));
    stat (fileNames[fcnt]->d_name, &info);
    strftime(dtBuf, 30, "%m/%d/%Y %H:%M:%S", localtime(&info.st_mtime));
    printf("%s %s\n", fileNames[fcnt]->d_name, dtBuf);
    fcnt++;
   }
  closedir( dirp );
  }
}
0
 

Author Comment

by:Loysi
ID: 7104304
Thanks woregul, for your quick response.
I changed the date format and got better results but still not correct.
The date and time of my file using ls -l
displays jun 23 2002 19:25
My program displays  12/31/1969 19:00:00
I guessing that is CUT (Coordinated Universal time).
What can I do to get it give me the same info as ls does.?
0
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!

 

Author Comment

by:Loysi
ID: 7104607
Message for Woregul, I retested the snippet and noticed it worked well for the root directory. recoded my program and added the path to the stat Function and it worked.
Got another question. for you. Can you tell me if there is an easy way to find files in a directory based on first in bases.? I'll double my points!
0
 

Accepted Solution

by:
woregul earned 300 total points
ID: 7104754
You could use scandir() to read the contents of a directory into an optionally sorted array of pointers to dirent structures. You would have to write your own compare function that compares 2 dirent structure pointers and see which one is newer. Once complete, you would have a sorted list by file date.

0
 

Author Comment

by:Loysi
ID: 7104859
Thanks Woregul, My very Thoughts, I was thinking in those very lines.
Thanks for your help. The solution was staring me in my face and I could not get it to work, I tried to use fstat and that gave me erratic results. I don't know why!  hence the question.
I doubled my points as promised.
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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

609 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