GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-May-2005, 04:12
Old_Spen Old_Spen is offline
New Member
 
Join Date: May 2005
Posts: 8
Old_Spen is on a distinguished road
Angry

dirent, stat, compar & alphasort


I was trying yo modify the directory listing example in the man pages to sort the files by time but I just don't seem to get how to do it.

Problem is that I ain't no C pro.

Need help urgently..... My code is as follows:

CPP / C++ / C Code:

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

main(){

struct stat fnm1, fnm2;
struct dirent a, b;
struct dirent **namelist;

int compar(struct dirent **pa, struct dirent **pb)
        {
        struct dirent a, b;
        a = **pa; b = **pb;
        return -strcmp(a.d_name, b.d_name);
}

/*
int alphasort (struct dirent **pa, sturct dirent **pb)
{
return strcmp((*pa)->d_name, (*pb)->d_name);
}
*/

int n;

   n = scandir(".", &namelist, 0, *compar);

   if (n < 0)
       perror("scandir");
   else {
   if (stat("(n->d_name)", &fnm1)==0 && stat("(n->d_name)", &fnm2) == 0)
        {
        double tdiff = difftime(fnm1.st_time, fnm2.st_time);
        while(n--) {
           printf("%s\n", namelist[n]->d_name);
           free(namelist[n]);
        }
      }

       free(namelist);
   }

}



As you can see, I even tried using the alphasort option but to no avail. Was using Turbo C but now trying to migrate to Gnu C

Any help would be nice.

Thanks

Old_Spen
Last edited by LuciWiz : 04-May-2005 at 04:29. Reason: The correct end tag is [/c]
  #2  
Old 05-May-2005, 00:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
To the tune of Wagner's Ride of the Valkries
That's Elmer Fudd's "Kill the Wabbit" to you youngsters...
Read the Sticky -- Read the Sticky -- ...

You need to give us more information... We can't see anything if you don't tell us where to look.
__________________

Age is unimportant -- except in cheese
  #3  
Old 06-May-2005, 14:13
Old_Spen Old_Spen is offline
New Member
 
Join Date: May 2005
Posts: 8
Old_Spen is on a distinguished road

Clarification


Quote:
Originally Posted by WaltP
To the tune of Wagner's Ride of the Valkries
That's Elmer Fudd's "Kill the Wabbit" to you youngsters...
Read the Sticky -- Read the Sticky -- ...

You need to give us more information... We can't see anything if you don't tell us where to look.

Well actually my program does not result in anything as I keep getting a warning on the illegal typecasting of a pointer to an int.

The original program for listing the directory contents in the linux manpages is:

CPP / C++ / C Code:

       /* print files in current directory in reverse order */
       #include <dirent.h>
       main(){
	   struct dirent **namelist;
	   int n;

	   n = scandir(".", &namelist, 0, alphasort);
	   if (n < 0)
	       perror("scandir");
	   else {
	       while(n--) {
		   printf("%s\n", namelist[n]->d_name);
		   free(namelist[n]);
	       }
	       free(namelist);
	   }
       }


What I am trying to do is adapt the above manpage example to display the file names according to when they were last modified or accessed using the functions for time, stat alphasort and the same thing using compar.

I think this is supposed to be done with scandir if I am not mistaken.

Hope this clarifies my needs.

Cheers,

Old_Spen
  #4  
Old 06-May-2005, 16:11
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Old_Spen
Well actually my program does not result in anything as I keep getting a warning on the illegal typecasting of a pointer to an int.
I hate to harp on the obvious, but read guideline 1.4b and 2.4. We can't help if we don't know where the error is....

Quote:
Originally Posted by Old_Spen
The original program for listing the directory contents in the linux manpages is:
CPP / C++ / C Code:

       /* print files in current directory in reverse order */
       #include <dirent.h>
       main(){
	   struct dirent **namelist;
	   int n;

	   n = scandir(".", &namelist, 0, alphasort);
	   if (n < 0)
	       perror("scandir");
	   else {
	       while(n--) {
		   printf("%s\n", namelist[n]->d_name);
		   free(namelist[n]);
	       }
	       free(namelist);
	   }
       }

1) Does that program work? If not, start by getting this one to work.
2) Is this the same as your program? If not, we obviously can't tell you what you did wrong in your program.

Quote:
Originally Posted by Old_Spen
What I am trying to do is adapt the above manpage example to display the file names according to when they were last modified or accessed using the functions for time, stat alphasort and the same thing using compar.

I think this is supposed to be done with scandir if I am not mistaken.
Since you didn't post any code using time(), stat(), alphasort(), nor compar() we can't tell what might be wrong.

Quote:
Originally Posted by Old_Spen
Hope this clarifies my needs.
Not a lot... Hope this clears up what we need to know to help you, though.
__________________

Age is unimportant -- except in cheese
  #5  
Old 11-May-2005, 00:58
Old_Spen Old_Spen is offline
New Member
 
Join Date: May 2005
Posts: 8
Old_Spen is on a distinguished road
Quote:
Originally Posted by WaltP
I hate to harp on the obvious, but read guideline 1.4b and 2.4. We can't help if we don't know where the error is....


1) Does that program work? If not, start by getting this one to work.
2) Is this the same as your program? If not, we obviously can't tell you what you did wrong in your program.


Since you didn't post any code using time(), stat(), alphasort(), nor compar() we can't tell what might be wrong.


Not a lot... Hope this clears up what we need to know to help you, though.

The man page code is:

CPP / C++ / C Code:
#include <stdio.h>
#include <dirent.h>

main(){
 struct dirent **namelist;
   int n;
   n = scandir(".", &namelist, 0, alphasort);
   if (n < 0)
       perror("scandir");
   else {
       while(n--) {
           printf("%s\n", namelist[n]->d_name);
           free(namelist[n]);
       }
       free(namelist);
   }
}

It returns this:


t.c
t
scandir.c
scandir
gemgeh.c
gemgeh
fork1.c
fork.c
fork
dsin1.c
dsin.c
dsin
..
.

****************************

My code to return directory contents based on time last modified is:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

main(){

struct stat fnm1, fnm2;
struct dirent a, b;
struct dirent **namelist;

int compar(struct dirent **pa, struct dirent **pb)
        {
        struct dirent a, b;
        a = **pa; b = **pb;
        return -strcmp(a.d_name, b.d_name);
}

/*
int alphasort (struct dirent **pa, sturct dirent **pb)
{
return strcmp((*pa)->d_name, (*pb)->d_name);
}
*/

int n;

   n = scandir(".", &namelist, 0, *compar);

   if (n < 0)
       perror("scandir");
   else {
   if (stat("(n->d_name)", &fnm1)==0 && stat("(n->d_name)", &fnm2) == 0)
        {
        double tdiff = difftime(fnm1.st_time, fnm2.st_time);
        while(n--) {
           printf("%s\n", namelist[n]->d_name);
           free(namelist[n]);
        }
      }

       free(namelist);
   }


}


The error I get from my code is:

Quote:
cc dsin.c -o dsin
dsin.c: In function `main':
dsin.c:30: warning: passing arg 4 of `scandir' from incompatible pointer type
dsin.c:37: structure has no member named `st_time'
dsin.c:37: structure has no member named `st_time'
make: *** [dsin] Error 1
  #6  
Old 11-May-2005, 08:46
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
As the message states, st_time isn't there because it doesn't need to be.
http://www.opengroup.org/onlinepubs/...ys/stat.h.html

Code:
DESCRIPTION The <sys/stat.h> header shall define the structure of the data returned by the functions fstat(), lstat(), and stat(). The stat structure shall contain at least the following members: dev_t st_dev Device ID of device containing file. ino_t st_ino File serial number. mode_t st_mode Mode of file (see below). nlink_t st_nlink Number of hard links to the file. uid_t st_uid User ID of file. gid_t st_gid Group ID of file. [XSI][Option Start] dev_t st_rdev Device ID (if file is character or block special). [Option End] off_t st_size For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link. [SHM][Option Start] For a shared memory object, the length in bytes. [Option End] [TYM][Option Start] For a typed memory object, the length in bytes. [Option End] For other file types, the use of this field is unspecified. time_t st_atime Time of last access. time_t st_mtime Time of last data modification. time_t st_ctime Time of last status change. [XSI][Option Start] blksize_t st_blksize A file system-specific preferred I/O block size for this object. In some file system types, this may vary from file to file. blkcnt_t st_blocks Number of blocks allocated for this object. [Option End]

What compiler are you using? Its documentation ought to tell you what stuff is there.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.