Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File browse question
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
uberwalla




PostPosted: Tue Jan 29, 2008 7:56 pm   Post subject: File browse question

I am kind of learning C so that i can program and compile programs for my psp (playstation portable)

anyways I would like to know how i could program this program i made in turing.

Turing:

var fileName : string
var attr : int
var dir : int
var dontcare : int %I set it as this because i dont need the variables set as this

dir := Dir.Open (".")

loop
    Dir.GetLong (dir, fileName, dontcare, attr, dontcare)
    exit when fileName = ""
    put fileName, " " ..
    if (attr and attrDir) not= 0 then
        put "[DIR]"
    else
        put ""
    end if
end loop
Dir.Close (dir)


anyways knowing how to code that would be awesome. Also the ability to go up and down directories would be bonus.

Thanks for taking a look
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: Wed Jan 30, 2008 11:05 am   Post subject: Re: File browse question

uberwalla; Take a look at http://www.softintegration.com/demos/chstandard/posix.html

It's close, but I'll leave it to you to modify that source to make the output more like your Turing example.

I find Ch to be a very capable C learning tool. It's interactive nature makes exploring the world of C and C++ much more exciting and involving. Check out the main site and then download yourself a copy of Ch Standard 5.5.

http://www.softintegration.com/products/chstandard/download/

Cheers,
Brian Tiffin
uberwalla




PostPosted: Wed Jan 30, 2008 3:49 pm   Post subject: Re: File browse question

thanks a lot brian. Quite helpful. I probably will have to modify it though as i am not really coding c programs for computer. More c for psp (playstation portable) so i may not have all the header files needed but we'll see.

Thanks again
uberwalla




PostPosted: Thu Jan 31, 2008 4:01 pm   Post subject: Re: File browse question

Hey again. I did it. Well kind of. Thanks again. I got most of what i wanted done except i have a few questions.

First of all here is what i got so far:

C:

#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>
#include <dirent.h>

#define printf pspDebugScreenPrintf

int exit_callback(int arg1, int arg2, void *common) {
        sceKernelExitGame();
        return 0;
}

int CallbackThread(SceSize args, void *argp) {
        int cbid;
        cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
        sceKernelRegisterExitCallback(cbid);
        sceKernelSleepThreadCB();
        return 0;
}

int SetupCallbacks(void) {
        int thid = 0;
        thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
        if(thid >= 0) {
                sceKernelStartThread(thid, 0, 0);
        }
        return thid;
}

PSP_MODULE_INFO("File Browser", 0, 1, 1);

int main() {
        pspDebugScreenInit();
        SetupCallbacks();
    DIR *dirp;
    struct dirent *direntp;

    dirp = opendir( "ms0:/PSP/" );
    while ( (direntp = readdir( dirp )) != NULL )
    {
                if(opendir(direntp->d_name) != NULL)
                {
                        printf( "%s [DIR]\n", direntp->d_name );
                }
                else
                {
                        printf( "%s\n", direntp->d_name );
                } 
    }
    closedir( dirp );
        sceKernelSleepThread();
    return 0;
}


Incase you were wondering the callbacks and the pspdebug +sceKernel things are just needed for the psp. Just to properly compile the program.

Anyways my problem is that my [DIR] doesn't show up properly. On my psp when i ran it it came out like this:

code:

. [DIR]
.. [DIR]
GAME150
GAME
SAVEDATA
COMMON
SYSTEM
THEME


at first it seemed all fine until it only showed up on the first two: "." and ".." when it should be showing up on all of them since its ran in a folder of all folders.

Anyways just wondering how come it only showed the "." and ".." as directories and none of the others, and how i may come to solve the problem.

Thanks again.
OneOffDriveByPoster




PostPosted: Thu Jan 31, 2008 8:30 pm   Post subject: Re: File browse question

uberwalla @ Thu Jan 31, 2008 4:01 pm wrote:
C:

//...
                if(opendir(direntp->d_name) != NULL)
//...

Check out <sys/stat.h> for S_ISDIR. In any case, you don't seem to be closing the directory...
uberwalla




PostPosted: Thu Jan 31, 2008 9:42 pm   Post subject: Re: File browse question

OneOffDriveByPoster @ Thu Jan 31, 2008 8:30 pm wrote:
In any case, you don't seem to be closing the directory...


that is what my post was also asking. Where to put the close dir part
btiffin




PostPosted: Thu Jan 31, 2008 11:15 pm   Post subject: Re: File browse question

Well, like OneOffDriveByPoster mentioned, look into the file mode bits and the _IFDIR flag and the S_ISDIR macro. You'll need to include an inner closedir call. And as to why the DIR is not printing; inside your else check the errno global. (Check some of the docs on opendir, readdir for the possible error conditions). errno is a C RTL hack, and for proper testing you usually have to explicitly set it to 0 before making calls that return error codes. This may not be the problem, but for debugging ... Smile

Your code is looking close. Just for fun, take a look in wikipedia for ls and read thru some of the source code listings. (Far more complicated than what you need, but you may be able to glean some more hints. I find it good practice to glance at wickedly complex code once and while, scanning for tidbits but letting your brain gloss over most of the details. Someday those details will all make sense - hopefully before the boss needs you to modify something "10 minutes ago".)

Cheers,
Brian
uberwalla




PostPosted: Fri Feb 01, 2008 5:06 pm   Post subject: Re: File browse question

thanks guys. very helpful, ill have to take a look at those. but as for the wikipedia idea, i think i should pass. I really don't care for big sites when starting to program a new language, because a lot of the code is too complicated for my level. I like to start small, then slowly progress.

Anyways ill try taking a look at some of the docs.

thanks.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: