PDA

View Full Version : Finding largest file in current directory?


AusTex
03-11-2005, 10:36 PM
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:

#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.

i3839
03-11-2005, 10:56 PM
Read the stat manpage, look for "st_size"...

sizeof(entry->d_name) will always return the same value, as it gives the size of d_name and not d_name's content, which you can get with a strlen().

AusTex
03-12-2005, 01:59 AM
Thanks for the help i3839. I have the program, now, finding the largest file in the current directory and displaying its size. But, I cant get the filename itself to display from the print statement: printf("The largest file in the current directory is %s\n", name); I can get filenames to display when going through the if loop that keeps track of the largest file. It seems to be some sort of scope issue or maybe a de-referencing problem. I was wondering if you could assist me with this or point me in the right direction.

Here is the updated code:


#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

void printfile(char *dir)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
struct stat statbuffer;
int i = 0;
char *name;

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)) {

lstat(entry->d_name, &statbuffer);

if(statbuffer.st_size > i) {
i = (statbuffer.st_size);
name = (entry->d_name);
// Names display fine here
printf("%s\n", name);
}
}

}

// Name will not display here
printf("The largest file in the current directory is %s\n", name);
printf("It is %i bytes\n", i);

closedir(dp);
}

int main()
{
printfile(".");
exit(0);
}

i3839
03-12-2005, 02:54 AM
Readdir uses one static fstat structure to fill file's info, so if you call it multiple times the name pointer will stay the same, but the content will change. Quote from the readdir manpage:

The data returned by readdir() may be overwritten by subsequent calls to readdir() for the same directory stream.

So you need to copy the name, either to a static buffer or dynamically with for instance strdup().

AusTex
03-12-2005, 03:48 AM
Awesome!! Thank you so much for your help. All is well and working now and, again, I really do appreciate the help.