How does one use qsort to sort a char containing pathnames/files based on their bytes?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I basically wrote a code in which I take two command line arguments one being the type of file that I want to search in my directory and they other being the amount I want(which is not implemented yet, but I can fix that)
The code is like so:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define sizeFileName 500
#define filesMax 5000
int cmpfunc( const void *a, const void *b)
return *(char*)a + *(char*)b;
int main( int argc, char ** argv)
FILE * fp = popen( "find . -type f", "r");
char * type = argv[1];
char * extension = ".";
char* tExtension;
tExtension = malloc(strlen(type)+1+4);
strcpy(tExtension, extension);
strcat(tExtension, type);
// printf("%sn",tExtension);
int amount = atoi(argv[2]);
//printf("%dn",amount);
char buff[sizeFileName];
int nFiles = 0;
char * files[filesMax];
while(fgets(buff,sizeFileName,fp))
int leng = strlen(buff) - 1;
if (strncmp(buff + leng - 4, tExtension, 4) == 0)
files[nFiles] = strndup(buff,leng);
//printf("t%sn", files[nFiles]);
nFiles ++;
fclose(fp);
printf("Found %d filesn", nFiles);
long long totalBytes = 0;
struct stat st;
// sorting based on byte size from greatest to least
qsort(files, (size_t) strlen(files), (size_t) sizeof(char), cmpfunc);
for(int i = 0;i< nFiles; i ++)
if(0!= stat(files[i],&st))
perror("stat failed:");
exit(-1);
totalBytes += st.st_size;
printf("%s : %ldn",files[i],st.st_size);
printf("Total size: %lldn", totalBytes);
// clean up
for(int i = 0; i < nFiles ; i ++ )
free(files[i]);
return 0;
So far I have every section set up properly, upon running the code say $./find ini 5,
it would print out all the ini files
followed by their byte size(it's currently ignore the 5). However, for the qsort()
, I'm not exactly sure how I would sort the contents of char * files
as while it holds the pathnames, I had to use stat
to get the byte sizes, how would I print out a sorted version of my print statements featuring the first statement being the most bytes and finishes at the least bytes?
directory sort c
New contributor
add a comment |Â
up vote
0
down vote
favorite
I basically wrote a code in which I take two command line arguments one being the type of file that I want to search in my directory and they other being the amount I want(which is not implemented yet, but I can fix that)
The code is like so:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define sizeFileName 500
#define filesMax 5000
int cmpfunc( const void *a, const void *b)
return *(char*)a + *(char*)b;
int main( int argc, char ** argv)
FILE * fp = popen( "find . -type f", "r");
char * type = argv[1];
char * extension = ".";
char* tExtension;
tExtension = malloc(strlen(type)+1+4);
strcpy(tExtension, extension);
strcat(tExtension, type);
// printf("%sn",tExtension);
int amount = atoi(argv[2]);
//printf("%dn",amount);
char buff[sizeFileName];
int nFiles = 0;
char * files[filesMax];
while(fgets(buff,sizeFileName,fp))
int leng = strlen(buff) - 1;
if (strncmp(buff + leng - 4, tExtension, 4) == 0)
files[nFiles] = strndup(buff,leng);
//printf("t%sn", files[nFiles]);
nFiles ++;
fclose(fp);
printf("Found %d filesn", nFiles);
long long totalBytes = 0;
struct stat st;
// sorting based on byte size from greatest to least
qsort(files, (size_t) strlen(files), (size_t) sizeof(char), cmpfunc);
for(int i = 0;i< nFiles; i ++)
if(0!= stat(files[i],&st))
perror("stat failed:");
exit(-1);
totalBytes += st.st_size;
printf("%s : %ldn",files[i],st.st_size);
printf("Total size: %lldn", totalBytes);
// clean up
for(int i = 0; i < nFiles ; i ++ )
free(files[i]);
return 0;
So far I have every section set up properly, upon running the code say $./find ini 5,
it would print out all the ini files
followed by their byte size(it's currently ignore the 5). However, for the qsort()
, I'm not exactly sure how I would sort the contents of char * files
as while it holds the pathnames, I had to use stat
to get the byte sizes, how would I print out a sorted version of my print statements featuring the first statement being the most bytes and finishes at the least bytes?
directory sort c
New contributor
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I basically wrote a code in which I take two command line arguments one being the type of file that I want to search in my directory and they other being the amount I want(which is not implemented yet, but I can fix that)
The code is like so:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define sizeFileName 500
#define filesMax 5000
int cmpfunc( const void *a, const void *b)
return *(char*)a + *(char*)b;
int main( int argc, char ** argv)
FILE * fp = popen( "find . -type f", "r");
char * type = argv[1];
char * extension = ".";
char* tExtension;
tExtension = malloc(strlen(type)+1+4);
strcpy(tExtension, extension);
strcat(tExtension, type);
// printf("%sn",tExtension);
int amount = atoi(argv[2]);
//printf("%dn",amount);
char buff[sizeFileName];
int nFiles = 0;
char * files[filesMax];
while(fgets(buff,sizeFileName,fp))
int leng = strlen(buff) - 1;
if (strncmp(buff + leng - 4, tExtension, 4) == 0)
files[nFiles] = strndup(buff,leng);
//printf("t%sn", files[nFiles]);
nFiles ++;
fclose(fp);
printf("Found %d filesn", nFiles);
long long totalBytes = 0;
struct stat st;
// sorting based on byte size from greatest to least
qsort(files, (size_t) strlen(files), (size_t) sizeof(char), cmpfunc);
for(int i = 0;i< nFiles; i ++)
if(0!= stat(files[i],&st))
perror("stat failed:");
exit(-1);
totalBytes += st.st_size;
printf("%s : %ldn",files[i],st.st_size);
printf("Total size: %lldn", totalBytes);
// clean up
for(int i = 0; i < nFiles ; i ++ )
free(files[i]);
return 0;
So far I have every section set up properly, upon running the code say $./find ini 5,
it would print out all the ini files
followed by their byte size(it's currently ignore the 5). However, for the qsort()
, I'm not exactly sure how I would sort the contents of char * files
as while it holds the pathnames, I had to use stat
to get the byte sizes, how would I print out a sorted version of my print statements featuring the first statement being the most bytes and finishes at the least bytes?
directory sort c
New contributor
I basically wrote a code in which I take two command line arguments one being the type of file that I want to search in my directory and they other being the amount I want(which is not implemented yet, but I can fix that)
The code is like so:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define sizeFileName 500
#define filesMax 5000
int cmpfunc( const void *a, const void *b)
return *(char*)a + *(char*)b;
int main( int argc, char ** argv)
FILE * fp = popen( "find . -type f", "r");
char * type = argv[1];
char * extension = ".";
char* tExtension;
tExtension = malloc(strlen(type)+1+4);
strcpy(tExtension, extension);
strcat(tExtension, type);
// printf("%sn",tExtension);
int amount = atoi(argv[2]);
//printf("%dn",amount);
char buff[sizeFileName];
int nFiles = 0;
char * files[filesMax];
while(fgets(buff,sizeFileName,fp))
int leng = strlen(buff) - 1;
if (strncmp(buff + leng - 4, tExtension, 4) == 0)
files[nFiles] = strndup(buff,leng);
//printf("t%sn", files[nFiles]);
nFiles ++;
fclose(fp);
printf("Found %d filesn", nFiles);
long long totalBytes = 0;
struct stat st;
// sorting based on byte size from greatest to least
qsort(files, (size_t) strlen(files), (size_t) sizeof(char), cmpfunc);
for(int i = 0;i< nFiles; i ++)
if(0!= stat(files[i],&st))
perror("stat failed:");
exit(-1);
totalBytes += st.st_size;
printf("%s : %ldn",files[i],st.st_size);
printf("Total size: %lldn", totalBytes);
// clean up
for(int i = 0; i < nFiles ; i ++ )
free(files[i]);
return 0;
So far I have every section set up properly, upon running the code say $./find ini 5,
it would print out all the ini files
followed by their byte size(it's currently ignore the 5). However, for the qsort()
, I'm not exactly sure how I would sort the contents of char * files
as while it holds the pathnames, I had to use stat
to get the byte sizes, how would I print out a sorted version of my print statements featuring the first statement being the most bytes and finishes at the least bytes?
directory sort c
directory sort c
New contributor
New contributor
New contributor
asked 2 hours ago
Li Wang
161
161
New contributor
New contributor
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Li Wang is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474394%2fhow-does-one-use-qsort-to-sort-a-char-containing-pathnames-files-based-on-their%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password