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.