Author Topic: [Help]Scan dir with Variable.  (Read 1434 times)

Rikku2000 (OP)

  • Posts: 48
[Help]Scan dir with Variable.
« on: June 07, 2010, 09:58:53 am »
I will make a emulator selection so i need a littel bit help here my exemple.
but it dont work do anyone have a idear?

Code: [Select]
struct dirent **namelist;
int n;

int dir_app;
int app;

n = scandir("/usr/local/apps/", &namelist, 0, alphasort);
if (n < 0) {
perror("scandir");
} else {
while (n--) {
if (namelist[n] == "gbc") {
dir_app = "/usr/local/apps/gbc/gnuboy4d";
app = "gnuboy4d";
} else if (namelist[n] == "gba") {
} else if (namelist[n] == "md") {
} else if (namelist[n] == "nes") {
} else if (namelist[n] == "psx") {
} else if (namelist[n] == "snes") {
} else {
}
}

free(namelist);
}

// Rikku2000: App load...
if (execl(dir_app, app, path, 0)) {
« Last Edit: June 07, 2010, 10:06:37 am by Rikku2000 »

8bitbubsy

  • Guest
Re: [Help]Scan dir with Variable.
« Reply #1 on: June 13, 2010, 06:33:37 am »
String comparison isn't as easy as comparing two pointer addresses... In C and C++, an array of chars returns a pointer to where it is stored in memory, so you are comparing two addresses. It's kind of non-sense

if (namelist[n] == "gbc")
should be
if (strcmp(namelist[n], "gbc") == 0)
(relates to all the other entries too, of course)
Also have case-sensitivity in mind. :)

The second error is that you are trying to store a pointer in a non-pointer integer, this is not what you want to do.
You should make your variables like this:

Code: [Select]
char dir_app[512];
char app[512];

Then to copy an array of chars (string) to it, you do this:
Code: [Select]
strcpy(dir_app, "/blabla/hello");
strcpy(app, "gnuboy");

C and C++ is a bit low-level, so you have to think twice if you're a beginner at it :)

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/


Something like this:
Code: [Select]
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>

int main(int argc, char *argv[])
{
struct dirent **namelist;
int numFiles, i;

/* not sure what alphasort is, it's prolly not defined */
numFiles = scandir("/usr/local/apps/", &namelist, 0, alphasort);
if (numFiles < 0)
{
perror("scandir");
return 1;
}

printf("Found %d objects:\n", numFiles);

for (i = 0; i < numFiles; i++)
{
printf("%03d: %s\n", i, namelist[i]->d_name);

/* Here you can make a file selector if you want */

if (strcmp(namelist[i]->d_name, "/boot/local/apps/hello.dge") == 0)
{
/* do something special ? Mark as an app? */
}
}

/* now we free it, so don't try to look it up anymore */
if (namelist != NULL)
{
free(namelist);
}

return 0;
}
« Last Edit: June 13, 2010, 06:58:30 am by 8bitbubsy »

 

Post a new topic
Post a new topic