The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
finding largest directories in a filesystem GKnight Shell Programming and Scripting 8 04-30-2008 05:58 PM
check the current date file in directory pallvi SUN Solaris 2 01-04-2008 02:57 PM
find the 5o largest files in a directory igidttam Filesystems, Disks and Memory 8 05-16-2007 10:20 AM
Unable to see all file in a current directory srikanthus2002 Shell Programming and Scripting 3 09-27-2006 01:07 AM
finding file in a directory legato UNIX for Dummies Questions & Answers 4 01-24-2005 08:27 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-11-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
Finding largest file in current directory?

I was hoping to get some assistance with this C program I am working on. The goal is to find the largest file in the current directory and then display this filename along with the filesize. What I have so far will display all the files in the current directory. But, how do I deal with "grabbing" and displaying the filesize?

So far:

Code:
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

void printfile(char *dir)
{
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;        

        if((dp = opendir(dir)) == NULL) {
           fprintf(stderr, "cannot open directory: %s\n", dir);
           return;

        }

        chdir(dir);
        while((entry = readdir(dp)) != NULL) {

           lstat(entry->d_name, &statbuf);

           if(S_ISREG(statbuf.st_mode)) {                
                 printf("%s\n", entry->d_name);
           }                

        }        
        closedir(dp);
}

int main()
{
        printfile(".");
        exit(0);
}
Shouldn't something like this work for filesize? It doesn't print the correct size.
Code:
 struct stat statbuffer;
struct dirent *entry;
if(S_ISREG(statbuffer.st_size){
int i = sizeof(entry->d_name);
printf("%d\n", i);}
Any help would be greatly appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 03-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Quote:
Originally Posted by AusTex
Shouldn't something like this work for filesize? It doesn't print the correct size.
Code:
 struct stat statbuffer;
struct dirent *entry;
if(S_ISREG(statbuffer.st_size){
int i = sizeof(entry->d_name);
printf("%d\n", i);}
Any help would be greatly appreciated.
You use S_ISREG on the st_mode entry just as your earlier code showed. You can't use it on anything else. Once you know you have a file, just display statbuffer.st_size. st_size is the size.

The sizeof operator just gives you the size of the variable...you can't do stuff like x=sizeof("Earth") to find out how big the Earth is.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0