Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 11th, 2005, 4:44 PM   #1
AusTex
Newbie
 
Join Date: Feb 2005
Posts: 7
Rep Power: 0 AusTex is on a distinguished road
Finding largest file?

I was hoping to get some assistance with this C program(Linux env.) 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:

#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.
 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.
AusTex is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC