![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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);
}
Code:
struct stat statbuffer;
struct dirent *entry;
if(S_ISREG(statbuffer.st_size){
int i = sizeof(entry->d_name);
printf("%d\n", i);}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|