Powered by Invision Power Board


  Reply to this topicStart new topicStart Poll

> Finding largest file?
AusTex
Posted: Mar 11 2005, 10:29 PM
Quote Post


Newbie
*

Group: Members
Posts: 3
Member No.: 836
Joined: 26-February 05



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.
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming/Languages | Next Newest »

Topic Options Reply to this topicStart new topicStart Poll

 



[ Script Execution time: 0.1894 ]   [ 12 queries used ]   [ GZIP Enabled ]




Partners: Cambridge Plus :: <Link Available> :: Stainless Steel Footswitch :: <Link Available>
Unix Man Pages / Linux Man Pages :: HiFi Forum :: SIP VoIP Phone & Provider Reviews :: UNIX/Linux Forum Archives

More info on advertising on Unix/Linux Forum