The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 08:58 PM
check the current date file in directory pallvi SUN Solaris 2 01-04-2008 05:57 PM
find the 5o largest files in a directory igidttam Filesystems, Disks and Memory 8 05-16-2007 01:20 PM
Unable to see all file in a current directory srikanthus2002 Shell Programming and Scripting 3 09-27-2006 04:07 AM
finding file in a directory legato UNIX for Dummies Questions & Answers 4 01-24-2005 11:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2005
AusTex AusTex is offline
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.
  #2 (permalink)  
Old 03-13-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,115
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.
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 On




All times are GMT -4. The time now is 08:23 AM.


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