The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
grep unix.com with google



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2002
Registered User
 

Join Date: Jul 2001
Location: Malaysia
Posts: 21
Listing File Info

Hi,

From a Unix book, i'd found that the way to list files in a directory.
But those file info are all not shown, wat is shown is only the file name.
Can anyone pls teach me how to show the file details?
(etc file size, read/write permission, modified date)

my code:

Dir *dirp;
struct dirent *direntp;

while((direntp = readdir(dirp)) != NULL)
printf("%s\n",direntp->d_name);

Thx for ur help!
  #2 (permalink)  
Old 11-07-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,146
Once you have the file name, use that with the lstat() system call. This will return you a all of the inode information in a stat structure. Then you can just print out the elements of the structure. It should be pretty easy. Take a look at the man page for lstat().
  #3 (permalink)  
Old 11-07-2002
Registered User
 

Join Date: Jul 2001
Location: Malaysia
Posts: 21
while ((direntp = readdir(dirp)) != NULL)
{
if (lstat(direntp->d_name, &statbuf) == -1){
fprintf("...");
else
printf("%s\t%s\n",direntp->d_name, statbuf.st_size);

}

Is it the way to do it?
  #4 (permalink)  
Old 11-08-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,146
Provided that you previously did a chdir() into this directory, the lstat will work. But look at the definition of the statbuf structure. You can't use a %s to display statbuf.st_size.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
file listing ... thepurple SUN Solaris 3 12-28-2007 09:39 AM
listing file with other format happyv Shell Programming and Scripting 3 06-26-2007 07:17 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 02:54 PM
Recursive directory listing without listing files psingh UNIX for Dummies Questions & Answers 4 05-10-2002 11:52 AM
File listing Astudent UNIX for Dummies Questions & Answers 5 10-25-2000 06:36 PM



All times are GMT -4. The time now is 01:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0