C function problem

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

C function problem

Post by jojojoris »

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: Select all

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: Select all

int main() {
  getdirentrys();
}
This works but is not practical in my situation

Code: Select all

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.
mjoey
Posts: 4
Joined: Mon Mar 31, 2008 2:36 am

Re: C function problem

Post by mjoey »

jojojoris wrote:

Code: Select all

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: Select all

// Globals
std&#58;&#58;vector<char*> dirlist, filelist;

// Function
void getDirEntries&#40;&#41;
&#123;
	int dfd;
	SceIoDirent dir;
	dfd = sceIoDopen&#40;"ms0&#58;/"&#41;;

	while &#40;sceIoDread&#40;dfd, &dir&#41; > 0&#41;
	&#123;
		if &#40;FIO_SO_ISREG&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			filelist.push_back&#40;dir.d_name&#41;;
		&#125;
			else if &#40;FIO_SO_ISDIR&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			dirlist.push_back&#40;dir.d_name&#41;;
		&#125;
	&#125;

	sceIoClose&#40;dfd&#41;;
&#125;

// Main
int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;

	getDirEntries&#40;&#41;;
	printf&#40;"Files&#58; %i\nDirectories&#58; %i\n", filelist.size&#40;&#41;, dirlist.size&#40;&#41;&#41;;

	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
I was unsure what struct you were using for fileinfo, but i'd recommend using SceIoDirent (for directories), and SceIoStat (for files).
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

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

the filename struct was this:

Code: Select all

typedef struct fileinfo&#123;
    char filename&#91;256&#93;;
    &#125;fileinfo;
mjoey
Posts: 4
Joined: Mon Mar 31, 2008 2:36 am

Post by mjoey »

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

the filename struct was this:

Code: Select all

typedef struct fileinfo&#123;
    char filename&#91;256&#93;;
    &#125;fileinfo;
Oh woops, overlooked that :(.

Code: Select all

void getDirEntries&#40;&#41;
&#123;
	int dfd, i = 0, j = 0;
	SceIoDirent dir;
	dfd = sceIoDopen&#40;"ms0&#58;/"&#41;;

	while &#40;sceIoDread&#40;dfd, &dir&#41; > 0&#41;
	&#123;
		if &#40;FIO_SO_ISREG&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			sprintf&#40;filelist&#91;i++&#93;.filename, "%s", dir.d_name&#41;;
		&#125;
			else if &#40;FIO_SO_ISDIR&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			sprintf&#40;dirlist&#91;j++&#93;.filename, "%s", dir.d_name&#41;;
		&#125;
	&#125;

	sceIoClose&#40;dfd&#41;;
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;

	getDirEntries&#40;&#41;;
	printf&#40;"File&#58; %s\nDir&#58; %s\n", filelist&#91;0&#93;.filename, dirlist&#91;0&#93;.filename&#41;;

	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
Worked for me. :)
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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

Jim
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

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.
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

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

the filename struct was this:

Code: Select all

typedef struct fileinfo&#123;
    char filename&#91;256&#93;;
    &#125;fileinfo;
Oh woops, overlooked that :(.

Code: Select all

void getDirEntries&#40;&#41;
&#123;
	int dfd, i = 0, j = 0;
	SceIoDirent dir;
	dfd = sceIoDopen&#40;"ms0&#58;/"&#41;;

	while &#40;sceIoDread&#40;dfd, &dir&#41; > 0&#41;
	&#123;
		if &#40;FIO_SO_ISREG&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			sprintf&#40;filelist&#91;i++&#93;.filename, "%s", dir.d_name&#41;;
		&#125;
			else if &#40;FIO_SO_ISDIR&#40;dir.d_stat.st_attr&#41;&#41;
		&#123;
			sprintf&#40;dirlist&#91;j++&#93;.filename, "%s", dir.d_name&#41;;
		&#125;
	&#125;

	sceIoClose&#40;dfd&#41;;
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;

	getDirEntries&#40;&#41;;
	printf&#40;"File&#58; %s\nDir&#58; %s\n", filelist&#91;0&#93;.filename, dirlist&#91;0&#93;.filename&#41;;

	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
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
User avatar
jbit
Site Admin
Posts: 293
Joined: Sat May 28, 2005 3:11 am
Location: København, Danmark
Contact:

Post by jbit »

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.....
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)
jbit wrote:What.....
I think he confused i++ with ++i :)

Ciaooo
Sakya
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

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: Select all

sceIoClose&#40;dfd&#41;;
Should be:

Code: Select all

sceIoDclose&#40;dfd&#41;;
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

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..
Post Reply