언어 설정

Menu
Sites
Language
List files in a directory using readdir of dirent.h

Hello to all,

I have a problem, well read list of files in a directory is not the proble, I have this code and works properly:

char buff[1024];
    ad->label = elm_label_add(ad->conform);
	sprintf(buff,"Hello ");

	struct dirent* dent;
		struct stat st;
		//if(( stat(DIR_MAIN, &st) != -1 ) && ( stat(DIR_TRK, &st) != -1 )){
			sprintf(buff,"%s 1", buff);
			DIR* srcdir = opendir(DIR_TRK);

			if (srcdir != NULL){
				sprintf(buff,"%s 2", buff);
				while((dent = readdir(srcdir)) != NULL){

			        //if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
			         //   continue;

			        //if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0){
			        if(stat(dent->d_name, &st))
			        	sprintf(buff,"%s, %s", buff,dent->d_name);

			    }
			closedir(srcdir);
			}
		  /*else
		    perror ("Couldn't open the directory");*/
		//}

	elm_object_text_set(ad->label, buff);
	evas_object_size_hint_weight_set(ad->label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_object_content_set(ad->conform, ad->label);

	/* Show window after base gui is set up */
	evas_object_show(ad->win);

But, this is only a test, the real target is put the file names in a Genlist, and I did this code:

static char *_item_label_get(void *data, Evas_Object *obj, const char *part)
{
    char *i = (char *) data;
	//char *buf=calloc(255, sizeof(char));
	if (!strcmp(part, "elm.text")) {
		//strncpy(buf, i, 254);

		//return buf;
		return strdup(i);
	}

	else return NULL;
}
char buff[255];

    	sprintf(buff,"Hello ");

		struct dirent* dent;
		struct stat st;
		//if(( stat(DIR_MAIN, &st) != -1 ) && ( stat(DIR_TRK, &st) != -1 )){
		
		DIR* srcdir = opendir(DIR_TRK);

		if (srcdir != NULL){
			
			while((dent = readdir(srcdir)) != NULL){

		        //if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
		         //   continue;

		        //if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0){
		        if(stat(dent->d_name, &st)){
		        	
		        	//strncpy(buff,dent->d_name, 254);
		        	//buff[254] = '\0';
				    elm_genlist_item_append(genlist,
				        		        		                            itc,
				        		        		                            (void *)dent->d_name,
				        													//(void *)buff,
				        		        		                            NULL,
				        		        		                            ELM_GENLIST_ITEM_NONE,
				        		        		                            NULL,
				        		        		                            NULL);

				    }
				}
				closedir(srcdir);
				}
			  /*else
			    perror ("Couldn't open the directory");*/
			//}



	elm_genlist_item_class_free(itc);
}

And is generated a list with number of elements of files in the directory, but no names! Any suggestion please!

Thanks to all!

답변 바로가기

Responses

1 댓글
Mark as answer
Carlos Dominguez

Ok, I founf the way!

First line was char buff[1024], this variable is in the stack and is going a be destroy after exit the "create genlist function", solution:

char *buff=(char*)malloc(255*sizeof(char));

I hope usefull.