LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2015, 11:31 PM   #1
dewin88
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Rep: Reputation: Disabled
Question Sort files and folders in some path on ubuntu 12.04 (C coding)


Hello.

I make this scructure for save information which i get:

typedef struct _FILE_STAT
{
char* file_name;
struct stat st;
} FILE_STAT;

Allocate an array of pointers to this structure, for example 16 pieces.

Started on the array counter to know its size and not get out.

Pass on the folder cycle and gather information in this array. If the array has appeared small, the cycle is increased it twice through realloc. And read on.

If i find the file to allocate memory for structure FILE_STAT. The pointer is placed in the array and increments the counter.

Next allocate memory for the file name and I bring to the resulting structure.

Need some sort functions for the name, size, and other data. I want to expose a function qsort depending on the type of sorting.

At the end of the cycle is to sort an array, set on a qsort.

Well, and display information.

How to implement it in C for linux? Sorry, i know only bash scripting.
 
Old 03-26-2015, 04:31 AM   #2
dewin88
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Code:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    struct stat file_stats;
    DIR *dirp;
    struct dirent* dent;

    dirp=opendir("."); // specify directory here: "." is the "current directory"
    do {
        dent = readdir(dirp);
        if (dent)
        {
            printf("%s  --  ", dent->d_name);
            if (!stat(dent->d_name, &file_stats))
            {
                printf("%u bytes\n", (unsigned int)file_stats.st_size);
                //how i can sort struct stat file_stats by bytes?
            }
            else
            {
                printf("(stat() failed for this file)\n");
            }
        }
    } while (dent);
    closedir(dirp);
}
find this code, but how i can sort by ascending and descending file size, which contain "bytes" ?
 
Old 03-26-2015, 04:37 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Call 'qsort' after closedir. Read 'man qsort' for details.
http://www.tutorialspoint.com/c_stan...tion_qsort.htm
 
Old 03-26-2015, 04:38 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
first you need to read all of the entries into an array and/or in a list and next you can sort that.
you can find some tips on sorting here: http://stackoverflow.com/questions/7...ef-struct-in-c
 
Old 03-26-2015, 09:42 AM   #5
dewin88
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
code

my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

int QUICKsort_VALUE(const void *sortI, const void  *sortJ)
  {
int sortI, sortJ;

   int sortI1=((const struct st *) sortI)->value;
   int sortJ2=((const struct st *) sortJ)->value;

   if (sortI1<sortJ2) {return -1;} else if (sortI1==sortJ2){return 0;} else {return 1;}

  }

void main(int argc, char* argv[])
{
DIR* d;
struct dirent * entry;
struct stat st;
if (argc != 2)
{
printf("Usage: ./sizelist directory\n");
return;
}
d = opendir(argv[1]);
if (d == NULL) {
printf("Directory not found\n");
return;
}
while ((entry = readdir(d)) != NULL)
{
stat(entry->d_name, &st);
if (S_ISREG(st.st_mode))
{
  printf("%u bytes\n", (unsigned int)st.st_size);
qsort(st_size, un, sizeof(struct st), QUICKsort_VALUE);

printf("%20s\t%d", entry->d_name, st.st_size);
}}
closedir(d); 
}
Why its not compile? I add function qsort and set the struct for sorting . what am I doing wrong? What do I need to correct in the code?
 
Old 03-26-2015, 09:52 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
at least post the error message you got (or any other information related to your question)
 
Old 03-27-2015, 12:36 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Why your code didn't compile? A lot of reasons, which you should review in the errors and warnings. This is just from a cut/paste of your code, therefore the first line is Line 1 and so forth, I didn't edit or modify what you posted:
Code:
gcc -o quick_sort quick_sort.c 
quick_sort.c: In function ‘QUICKsort_VALUE’:
quick_sort.c:8:5: error: ‘sortI’ redeclared as different kind of symbol
quick_sort.c:6:33: note: previous definition of ‘sortI’ was here
quick_sort.c:8:12: error: ‘sortJ’ redeclared as different kind of symbol
quick_sort.c:6:53: note: previous definition of ‘sortJ’ was here
quick_sort.c:10:42: error: dereferencing pointer to incomplete type
quick_sort.c:11:42: error: dereferencing pointer to incomplete type
quick_sort.c: In function ‘main’:
quick_sort.c:38:7: error: ‘st_size’ undeclared (first use in this function)
quick_sort.c:38:7: note: each undeclared identifier is reported only once for each function it appears in
quick_sort.c:38:16: error: ‘un’ undeclared (first use in this function)
quick_sort.c:38:27: error: invalid application of ‘sizeof’ to incomplete type ‘struct st’ 
quick_sort.c:40:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘__off_t’ [-Wformat]
I go backwards because when you fix stuff it changes the line numberings so I usually start at the end and fix backwards when most plausible:

Line 40: You need to look up what the contents of a struct stat structure is, because st_size is not an integer, or you need to cast.

Line 38, column 7: You can't figure out what's wrong there? It's a typing mistake.
Line 38, column 16: Should be equally obvious
Line 38, column 27: Because it should be sizeof(struct stat) or sizeof(st)

Lines 6, 8, 10, and 11: The main issues here are that to take generic pointers and cast them to a structure, you should declare the local pointers to match what you wish to cast it too. You cannot take passing arguments of a function and reuse those names. The errors on line 8 are complaining that you're attempting to redefine the variable types of the function's passing arguments.

Suggestions for that section:
Code:
int QUICKsort_VALUE(void *sortI, void *sortJ)
{
    struct st *pSortI, *pSortJ;
    int sortIVal, sortJVal;

    pSortI = (struct st *)sortI;
    pSortJ = (struct st *)sortJ;

    sortIVal = pSortI->value;
    sortJVal = pSortJ->value;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to hide files/folders ubuntu 12.04 zeeshanayub Linux - Newbie 4 02-16-2014 07:19 AM
[SOLVED] Script to sort video files into different folders MidKnightLoki Linux - General 1 11-20-2012 05:49 PM
How do I get full permission to all files and folders in Ubuntu? Chuckles278 Linux - Newbie 19 05-16-2011 10:35 PM
I have no coding or programming experience of any sort. Is Linux a good idea for me? taloncrewchief Linux - Newbie 13 11-26-2009 11:34 PM
Deleting files/folders in Ubuntu 7.04 gwynn Linux - Newbie 5 06-07-2007 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration