Link to home
Start Free TrialLog in
Avatar of nearlyanexpert
nearlyanexpert

asked on

Retrieve the file listing in a certain directory

Hi,

As the title says please. However I would like it to be platform independant, if this isn't possible then Linux please. I would like each file to be in a seperate string and not a HUGE string containing every file.

Thanks

quesiton by question I'm nearlyanexpert!
Avatar of akshayxx
akshayxx
Flag of United States of America image

for plat form independent solution u'll have to write your own code .. i doesnt mean it doesnt exist . but u better work it out urself ..
till then have a look at this  solution for linux..
the only thing is that it lists file in reverse order
in current directory .. u can modify it for custom directory
and also to list in original order .. ( by incrementing n , instead of decrementing it)..

      #include <dirent.h>
       main(){
           struct dirent **namelist;
           int n;

           n = scandir(".", &namelist, 0, alphasort);
           if (n < 0)
               perror("scandir");
           else {
               while(n--) {
                   printf("%s\n", namelist[n]->d_name);
                   free(namelist[n]);
               }
               free(namelist);
           }
       }
if u have hold of ANSI C book by kernighan ritchie .. they have full implementation of directory listing routine.. they have defined their own dirent.h structures and all
i guess that can be applied to almost every platform .. with slight modifications though.
here is one for windows

#include <iostream.h>
#include <windows.h>


    int GetDir(char* dPath) {
   
    WIN32_FIND_DATA FileData;
    HANDLE hFile;
    hFile = FindFirstFile(dPath,&FileData);
   


        if ( INVALID_HANDLE_VALUE == hFile ) {
        cout << "No files" << endl;
        return false;
        }


            for ( ;; ){
            cout << FileData.cFileName << endl;
            if ( 0 == FindNextFile(hFile, &FileData ) )
            break;
            }
            return true;
        }



            int main() {
            GetDir("c:\\*.txt");
            return true;
        }
Avatar of Narendra Kumar S S
What are you upto akshayxx!? You are giving the complete code without asking!!! Don't you think you are doing somebody else's homework!?:-))

-Narendra
I didnt write the codes .. the first one is from  MAN page of scandir. and the second one is from  some already asked question on EE  :)..
ASKER CERTIFIED SOLUTION
Avatar of marcjb
marcjb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mindphaser
Mindphaser

nearlyanexpert

This question was brought to our attention. There are concerns that you ask our experts to do your homework which would be against our clause for academic honesty (https://www.experts-exchange.com/jsp/infoMemberAgreement.jsp)

Please let us know why you need this functionality.

** Mindphaser - Community Support Moderator **
Avatar of nearlyanexpert

ASKER

Hi,

Thanks everybody for your response (both positive and negative!). Yeah...I had a feeling that it would be classed as ssnkumar says, 'doing somebodys homework'. I wish it was my homework!!! The most we get to learn at college is Delphi :(. Basically, I'm wanting to try to write a file browser primarliy for Linux. I just miss some of the funcitonality in Nautilus, as I get in windows. I also want to add in a few things of my own. Also I just think it would be a great way to learn a bit more about GTK/GLib etc...

Hope I've clarified my post! :)

Thanks
nearlyanexpert
Hrm....actually could you please explain what classes as doing somebody's homework and what doesn't..How do you judge it? I mean the question I asked from my view is completely legit, however I do accept that it may sound like me wanting homework help. But I'm not quite sure how else I can ask it, as I don't even have a clue where to start with it. I don't have to have the entire code to the problem do I? The simple idea which marcjb was quite enough!

The reason I ask is I don't want to give an impression of wanting people to do my homework for me in the future.

Thanks again,
nearlyanexpert
so if u need to have a file browser using GTK .. it already exists there.. u just have to use filebrowser widget ..
u can easily find demo for file browser in the installed demos with the GTK-devel package
and since u'll be using GTK's widget . and if u have gtk for both windows and linux .. then u dont have to worry about the OS dependency, that GTK will take care of
but isn't a filebrowser widget just something like the file open/close panel, where you just select the file and click ok?

Where do I get these demos from? I'm using RedHat 8.0, and for the life of me just can't find them...if I have them at all that is!
Well, I try not to post code unless the question itself has code in it.  Then, I may post corrections, or another way of doing something.

In your case, you asked how something could be done, so I mentioned the functions that you would need.  I left it to you to implement it.  Now if you came back with some code and still had a question, I would gladly help.  If you had just responded, "Yeah, thanks for the tip, but how do I write the function", then it sounds more like you aren't really working towards the solution, but just hoping someone will do it for you.

It may help in the future to comment on some of the things you have tried, or in this case, explicitly state that you don't need code and are just interested in what functions (if any) can help you solve your problem.  That is, you are asking to be pointed in the right direction.

I hope you get some other answers to this, as I am curious as to what some of the other members think.  Good luck,

Marc
well if u installed development libraries .. then u can find the demos under this directory
/usr/share/doc/gtk2-devel-2.0.6/examples

check the exact version numbers

OR do this

rpm -qa |grep gtk |grep devel
and see whats the package name of gtk2-devel
then with that name issue this command
rpm -ql gtk2-devel-2.0.6 check version from the above rpm -qa|grep combination

then u can see where are those examples

and about gtk filebrowser widget .. well its more than just fopen and fread.. and list ..
i mean when u are concerned about listing and browsing the directories as a PART of ur main application  then u shud better use already available widgets for that.. they are more optimised .. thus u'll save time and u can use that for concentrating at more important job at hand

well as for my comments of this being a homework.. .. when  i saw his query of how to  get file listings .. and that too platform independent .. i thought he isnt doing just homework .. in homeworks they dont ask for platfrom independence .. and that too with c/c++...

so i found it appropriate to show him the right pointer for the simplest possible code for the directory listing .. which i didnt write myself .. on linux that can be seen in man page of scandir .. and for the windows one also i didnt have to look any further .. ( i dont work on windows usually ) so i cannot have written windows-code for him..


may be he was too lazy  to put his requirements more clearly
Thanks akshayxx. I'll check out the filebrowser widget. I'm currently just looking into the way in which marcjb mentioned first. I will accept either yours or marcjb's comment here and make another comment with some more points for the other person, as both methods seem quite nice. Although before I accept any question I'd better wait for my accuser and Mindphaser to accept that this was a legit quesiton :)

nearlyanexpert
I wasn't being lazy. I wasn'tquite sure what I wanted and I only had a rough idea of what I wanted and that was the best I could come up with as far as explaination goes...Sorry for not making myself more clear in the first place!

nearlyanexpert
WOAW!!!!!!!! THESE TUTORIALS ARE EXCELLENT!!! :D:D:D

Thanks again akshayxx!
haha .. explore that /usr/share directory ..
btw FYI .. if u did full install of redhat8.0 then for most of the things u need not look around.. u'll find almost all programming resources on ur box itself...
go explore it :)

btw did u try this command
gtk-demo
u'll see list of demos .. double click each of them.. and on the right plane u can see the corresponding source code

and also .. if u have made up ur mind on gtk-gui development
then have you heard of
glade

type this command
and u'll have drag and drop gui builder .. which even generates c-source code for the WYSIWYG.. gui.. its pretty quick and a lot of features. callbacks and all the stuff u need to develop gui application
yeah I used Glade a bit, although I found an IDE of it, Anjuta. I think its http://www.anjuta.net. If its not its: http://anjuta.sourceforge.net

nearlyanexpert
that also sounds nice .. i'll look at it .. and the good thing is that its developer team is indian .. my country :)
hmm not all of the team . but the lead developer is indian .. plus couple of others :)
I'm pleased I've helped!
marc...can you tell me what POSIX is?
Actually doesn't matter about that last question Marc.

Neither the moderator or my accuser has replied yet so I'm going to close this thread.

Thanks all!
nearlyanexpert
nearlyanexpert

The homework question is a tough one. The Member Agreement only talks about 'academical honesty'.
In a nutshell: If somebody posts the assigment and asks me to do his work, then it is against our policy. If you post your assignment AND the work you did alraedy and ask a specific question about that work ("Why do I get a segmentation error with that code?") our experts are most of the time very helpful. You see the difference? You do  the work, we get you on the right track :-)

** Mindphaser - Community Support Moderator **

Ok, I'll try to remember for next time!

nearlyanexpert
nearlyanexpert,

This is from a couple of different websites, and I think it answers what POSIX is:

What is POSIX?
POSIX is the Portable Operating System Interface, the open operating interface standard accepted world-wide. It is produced by IEEE and recognized by ISO and ANSI.

POSIX support assures code portability between systems and is increasingly mandated for commercial applications and government contracts. For instance, the USA's Joint Technical Architecture—Army (JTA-A) standards set specifies that conformance to the POSIX specification is critical to support software interoperability.


Hope this helps, and good luck,

Marc
Sorry, if I have affended anybody....:-(
Nowadays we are seeing lot of homework postings in EE.
So, it will be better we make sure that we are not doing somebody's homework and depriving him of from learning the skills......
I am not saying that we must not help in doing homeworks! We must help them, but, we must not do it ourselves!:-))

nearlyanexpert and akshayxx, I am very sorry to have classified this posting as homework and accused you of solving a homework by giving the code......
Hope you will excuse me......

-Narendra
Folks

Good job here. I think you all solved that moral issue pretty well (I've seen other threads where bad words started flying). As I said, the homework question is tough, but I hope we will get some better guidelines what is acceptable and what not.

** Mindphaser - Community Support Moderator **
No offence taken! Don't think anything of it, I understand its better to be safe than sorry! :)

nearlyanexpert