forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C function problem

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
jojojoris



Joined: 30 Mar 2008
Posts: 261

PostPosted: Tue Apr 01, 2008 3:37 am    Post subject: C function problem Reply with quote

I'm using some global variables because i hate using pointers.

I have program a function which put the names of the directories and files in a structure.

the part with the function and global variale declaration:

Code:
fileinfo filelist[128];
fileinfo dirlist[128];
int dirs,files;

void getdirentrys(void){
    dirs=0;
    files=0;
   int dfd;
    SceIoDirent dir;
    memset(&dir, 0, sizeof dir);
   dfd = sceIoDopen("ms0:/");
   while(sceIoDread(dfd, &dir)>0){
           if (FIO_SO_ISREG(dir.d_stat.st_attr)){
              sprintf(filelist[files].filename,"%s",dir.d_name);
               files++;
           }
           else if (FIO_SO_ISDIR(dir.d_stat.st_attr)){
               sprintf(dirlist[dirs].filename,"%s",dir.d_name);
               dirs++;
           }
          
       }
    sceIoClose(dfd);
}


In the main function i call this function but then it does nothing
If i put the content of this function direct into the main it works fine but that isn't practical in my situation.

This dont work:
Code:
int main() {
  getdirentrys();
}

This works but is not practical in my situation
Code:
int main() {
    dirs=0;
    files=0;
   int dfd;
    SceIoDirent dir;
    memset(&dir, 0, sizeof dir);
   dfd = sceIoDopen("ms0:/");
   while(sceIoDread(dfd, &dir)>0){
           if (FIO_SO_ISREG(dir.d_stat.st_attr)){
              sprintf(filelist[files].filename,"%s",dir.d_name);
               files++;
           }
           else if (FIO_SO_ISDIR(dir.d_stat.st_attr)){
               sprintf(dirlist[dirs].filename,"%s",dir.d_name);
               dirs++;
           }
          
       }
    sceIoClose(dfd);
}


What do i wrong?

Excude for my bad english.
Back to top
View user's profile Send private message
mjoey



Joined: 31 Mar 2008
Posts: 4

PostPosted: Tue Apr 01, 2008 5:05 am    Post subject: Re: C function problem Reply with quote

jojojoris wrote:

Code:

void getdirentrys(void)


I didn't try your example (i'm about to do it), but i'm pretty sure you can't do that. (referring to the void in parenthesis)

Also you did 'sizeof dir' which I don't think is allowed.


*edit* Alright after doing some minor-testing.. I seemed to be able to get what you 'seem' to be wanting.

Code:

// Globals
std::vector<char*> dirlist, filelist;

// Function
void getDirEntries()
{
   int dfd;
   SceIoDirent dir;
   dfd = sceIoDopen("ms0:/");

   while (sceIoDread(dfd, &dir) > 0)
   {
      if (FIO_SO_ISREG(dir.d_stat.st_attr))
      {
         filelist.push_back(dir.d_name);
      }
         else if (FIO_SO_ISDIR(dir.d_stat.st_attr))
      {
         dirlist.push_back(dir.d_name);
      }
   }

   sceIoClose(dfd);
}

// Main
int main(int argc, char *argv[])
{
   SetupCallbacks();
   pspDebugScreenInit();

   getDirEntries();
   printf("Files: %i\nDirectories: %i\n", filelist.size(), dirlist.size());

   sceKernelSleepThread();
   return 0;
}


I was unsure what struct you were using for fileinfo, but i'd recommend using SceIoDirent (for directories), and SceIoStat (for files).
Back to top
View user's profile Send private message
jojojoris



Joined: 30 Mar 2008
Posts: 261

PostPosted: Tue Apr 01, 2008 6:43 am    Post subject: Reply with quote

I use c not c++ so i can't use #include <vector>

the filename struct was this:

Code:
typedef struct fileinfo{
    char filename[256];
    }fileinfo;
Back to top
View user's profile Send private message
mjoey



Joined: 31 Mar 2008
Posts: 4

PostPosted: Tue Apr 01, 2008 10:09 am    Post subject: Reply with quote

jojojoris wrote:
I use c not c++ so i can't use #include <vector>

the filename struct was this:

Code:
typedef struct fileinfo{
    char filename[256];
    }fileinfo;


Oh woops, overlooked that :(.

Code:

void getDirEntries()
{
   int dfd, i = 0, j = 0;
   SceIoDirent dir;
   dfd = sceIoDopen("ms0:/");

   while (sceIoDread(dfd, &dir) > 0)
   {
      if (FIO_SO_ISREG(dir.d_stat.st_attr))
      {
         sprintf(filelist[i++].filename, "%s", dir.d_name);
      }
         else if (FIO_SO_ISDIR(dir.d_stat.st_attr))
      {
         sprintf(dirlist[j++].filename, "%s", dir.d_name);
      }
   }

   sceIoClose(dfd);
}

int main(int argc, char *argv[])
{
   SetupCallbacks();
   pspDebugScreenInit();

   getDirEntries();
   printf("File: %s\nDir: %s\n", filelist[0].filename, dirlist[0].filename);

   sceKernelSleepThread();
   return 0;
}


Worked for me. :)
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Tue Apr 01, 2008 10:45 pm    Post subject: Reply with quote

SceIoDirent dir;
You need to clear dir else it will sometimes crash
SceIoDirent dir = {0};
fixes that.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
jojojoris



Joined: 30 Mar 2008
Posts: 261

PostPosted: Tue Apr 01, 2008 11:35 pm    Post subject: Reply with quote

Thanks.

you write a much better function than my.
I also found out why my first function didn't work.
In the main function i accidentally set files and dirs back to zero after calling the getdirentrys function. So my program prints no output becouse it tought there were no entries.

But i'm going to use your function becouse that one is much better.
Back to top
View user's profile Send private message
PosX100



Joined: 15 Aug 2007
Posts: 98

PostPosted: Wed Apr 02, 2008 8:55 pm    Post subject: Reply with quote

mjoey wrote:
jojojoris wrote:
I use c not c++ so i can't use #include <vector>

the filename struct was this:

Code:
typedef struct fileinfo{
    char filename[256];
    }fileinfo;


Oh woops, overlooked that :(.

Code:

void getDirEntries()
{
   int dfd, i = 0, j = 0;
   SceIoDirent dir;
   dfd = sceIoDopen("ms0:/");

   while (sceIoDread(dfd, &dir) > 0)
   {
      if (FIO_SO_ISREG(dir.d_stat.st_attr))
      {
         sprintf(filelist[i++].filename, "%s", dir.d_name);
      }
         else if (FIO_SO_ISDIR(dir.d_stat.st_attr))
      {
         sprintf(dirlist[j++].filename, "%s", dir.d_name);
      }
   }

   sceIoClose(dfd);
}

int main(int argc, char *argv[])
{
   SetupCallbacks();
   pspDebugScreenInit();

   getDirEntries();
   printf("File: %s\nDir: %s\n", filelist[0].filename, dirlist[0].filename);

   sceKernelSleepThread();
   return 0;
}


Worked for me. :)


You should set , i & j = -1, because here:
sprintf(filelist[i++].filename, "%s", dir.d_name);
sprintf(dirlist[j++].filename, "%s", dir.d_name);

index "0" always remains null...

So , i = -1, j= -1
Back to top
View user's profile Send private message
jbit
Site Admin


Joined: 28 May 2005
Posts: 293
Location: København, Danmark

PostPosted: Wed Apr 02, 2008 9:06 pm    Post subject: Reply with quote

PosX100 wrote:
You should set , i & j = -1, because here:
sprintf(filelist[i++].filename, "%s", dir.d_name);
sprintf(dirlist[j++].filename, "%s", dir.d_name);

index "0" always remains null...

So , i = -1, j= -1

What.....
Back to top
View user's profile Send private message Visit poster's website
sakya



Joined: 28 Apr 2006
Posts: 190

PostPosted: Wed Apr 02, 2008 9:18 pm    Post subject: Reply with quote

Hi! :)
jbit wrote:
What.....

I think he confused i++ with ++i :)

Ciaooo
Sakya
Back to top
View user's profile Send private message Visit poster's website
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Wed Apr 02, 2008 9:19 pm    Post subject: Reply with quote

PosX100 wrote:
You should set , i & j = -1, because here:
sprintf(filelist[i++].filename, "%s", dir.d_name);
sprintf(dirlist[j++].filename, "%s", dir.d_name);

index "0" always remains null...

So , i = -1, j= -1


Although that may be true for ++i and ++j, it's not true for i++ and j++.

On a further note:

Code:
sceIoClose(dfd);


Should be:

Code:
sceIoDclose(dfd);
Back to top
View user's profile Send private message
PosX100



Joined: 15 Aug 2007
Posts: 98

PostPosted: Wed Apr 02, 2008 9:22 pm    Post subject: Reply with quote

jbit wrote:
PosX100 wrote:
You should set , i & j = -1, because here:
sprintf(filelist[i++].filename, "%s", dir.d_name);
sprintf(dirlist[j++].filename, "%s", dir.d_name);

index "0" always remains null...

So , i = -1, j= -1

What.....



Probably i typed something stupid.

Anyway,what i was trying to say , is that:

i=j=0
on loop 1 : i++ = 1 , j++ = 1
on loop 2 : i++ = 2 , j++ = 2

what actually happens :
on loop 1 : i++ = 0 , j++ = 0
on loop 2 : i++ = 1 , j++ = 1

Sorry :P

edit:
Sakya& "insert_witty_name"...you're right..
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group