How does one use qsort to sort a char containing pathnames/files based on their bytes?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question







New contributor




Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    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?










    share|improve this question







    New contributor




    Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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?










      share|improve this question







      New contributor




      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor




      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      Li Wang

      161




      161




      New contributor




      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Li Wang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Li Wang is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          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



































          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.









           

          draft saved


          draft discarded


















          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.













           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay