my string array is not working correctly with my filebrowser

Post Reply
Kazo
Posts: 14
Joined: Mon Aug 29, 2011 8:06 am

my string array is not working correctly with my filebrowser

Post by Kazo » Mon Aug 29, 2011 8:38 pm

I am trying to copy the string of pent->d_name to an array of strings.

here is the code.

Code: Select all

void ReadDir()
{
	fileCount = 0;

	DIR *pdir;
	struct dirent *pent;
	struct stat statbuf;
	pdir=opendir(currentDir);
	if (pdir)
	{
		while ((pent=readdir(pdir))!=NULL)
		{
			stat(pent->d_name,&statbuf);

	    	if(S_ISDIR(statbuf.st_mode))
			{
				DirList[fileCount] = pent->d_name;
				fileCount++;
			}

	    	if(!(S_ISDIR(statbuf.st_mode)))
			{
				DirList[fileCount] = pent->d_name;
				fileCount++;
			}
		}
		closedir(pdir);
		fileCount--;
	}
	else
	{
		iprintf ("opendir() failure; terminating\n");
	}
}
somehow all the strings in the array are all the same string.

Code: Select all

for(i = 0; i <= fileCount; i++)
	{
		if(i == currentFile)
		{
			currentFilestr = "*";
		}
		else
		{
			currentFilestr = " ";
		}
		iprintf("%s%s\n", currentFilestr, DirList[i]);
	}
Image

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: my string array is not working correctly with my filebro

Post by mtheall » Mon Aug 29, 2011 9:11 pm

I think you don't understand what this line does:

Code: Select all

DirList[fileCount] = pent->d_name;
This does not copy the contents of one string into another.

Kazo
Posts: 14
Joined: Mon Aug 29, 2011 8:06 am

Re: my string array is not working correctly with my filebro

Post by Kazo » Tue Aug 30, 2011 2:57 am

sorry, I am used to C#

I changed it to strcpy(DirList[fileCount], pent->d_name);

and now i get this.

Image

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: my string array is not working correctly with my filebro

Post by Discostew » Tue Aug 30, 2011 3:48 am

Sorry to be blunt, but you should probably read up on some of the basics of C. We don't even know what type DirList is, or if it is an array.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: my string array is not working correctly with my filebro

Post by mtheall » Tue Aug 30, 2011 4:08 am

You need to do something like this:

Code: Select all

char * DirList[SIZE];
...
DirList[filecount] = malloc(strlen(pent->d_name)+1);
if(DirList[filecount] != NULL)
  strcpy(DirList[filecount], pent->d_name);
filecount++;
Don't forget to free() all the stuff you malloc'd when you are done with them.

Code: Select all

for(i = 0; i < filecount; i++)
{
  if(DirList[i] != NULL)
  {
    free(DirList[i]);
    DirList[i] = NULL;
  }
}
Also, before you ask more questions about string manipulations and printing them on the DS, you should try these things out in a PC program. And of course, you would try to find the answers to these questions in general C forums, as this is nowhere near DS-specific.

Kazo
Posts: 14
Joined: Mon Aug 29, 2011 8:06 am

Re: my string array is not working correctly with my filebro

Post by Kazo » Tue Aug 30, 2011 8:03 am

thanks mtheall.

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: my string array is not working correctly with my filebro

Post by WinterMute » Tue Aug 30, 2011 11:12 am

mtheall wrote:You need to do something like this:

Code: Select all

char * DirList[SIZE];
...
DirList[filecount] = malloc(strlen(pent->d_name)+1);
if(DirList[filecount] != NULL)
  strcpy(DirList[filecount], pent->d_name);
filecount++;
Don't forget to free() all the stuff you malloc'd when you are done with them.

Code: Select all

DirList[filecount] = strdup(pent->d_name);
is much more readable and does the same thing, you'll still need to free the allocated memory when you're finished.
Help keep devkitPro toolchains free, Donate today

Personal Blog

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: my string array is not working correctly with my filebro

Post by mtheall » Tue Aug 30, 2011 3:09 pm

That works, too. Just make sure you handle the scenario where it can't allocate memory for the string (strdup will return NULL).

Post Reply

Who is online

Users browsing this forum: No registered users and 77 guests