LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2003, 04:48 PM   #1
PsychosisNode
Member
 
Registered: Apr 2003
Location: Reading, Berkshire, IN-GER-LUND!!!
Distribution: Mandriva 2006
Posts: 228

Rep: Reputation: 30
Listing files from within a C program?


C'mon, you can help a n00b who can't even do this (please!):

How does one list files from within a C program?

What I need to do is to get a list of filenames in a directory that end with a particular extension, so I can put them into a GTK+ combobox for selection...

Thanks in advance,

-Taiyo
 
Old 11-15-2003, 05:06 PM   #2
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
man opendir
man readdir
 
Old 11-15-2003, 08:06 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
assume you want to look in the current dir "." for files like *.tmp
Code:
#include <dirent.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[]){
	   struct dirent **namelist;
	   int n;

	   n = scandir(".", &namelist, 0, alphasort);
	   if (n < 0)
	       perror("scandir");
	   else {
	       while(n--) {
                                   if(strstr(namelist[n]->d_name,".tmp")
		            printf("%s\n", namelist[n]->d_name);
		   free(namelist[n]);
	       }
	       free(namelist);
	   }
                  return 0;
}
 
Old 11-15-2003, 08:07 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
PS: that would be:
man scandir, not man opendir
 
Old 11-16-2003, 10:40 AM   #5
PsychosisNode
Member
 
Registered: Apr 2003
Location: Reading, Berkshire, IN-GER-LUND!!!
Distribution: Mandriva 2006
Posts: 228

Original Poster
Rep: Reputation: 30
Thanks!

Thakyou people! This is working so far...

Code:
  int count = 0;
  GList *iwads = NULL;
  iwad_dir = opendir( "/usr/local/share/games/doom" );
  while( NULL != ( iwad_dir_entry = readdir( iwad_dir ) ) ){
    sprintf( tempstring[count], "%s", iwad_dir_entry->d_name);
    iwads = g_list_append( iwads, (gpointer)&tempstring[count] );
    count++;
  }
  closedir( iwad_dir );


  select_iwad = gtk_combo_new();
  gtk_combo_set_popdown_strings( GTK_COMBO( select_iwad ), iwads );
  gtk_combo_set_value_in_list( GTK_COMBO( select_iwad ), FALSE, FALSE );
  gtk_box_pack_start( GTK_BOX( vbox1 ), select_iwad, TRUE, TRUE, 0 );
  gtk_widget_show( select_iwad );
/me wonders who can guess what he's coding?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
listing all files of a certain user X-GhoSt Linux - General 1 10-22-2005 03:22 AM
listing files in a directory Linh Programming 7 05-17-2004 04:35 AM
listing large files fintan Linux - Newbie 7 11-06-2003 11:40 AM
Font listing program? Jiawen Linux - Software 2 09-23-2003 11:36 PM
listing files in apache im1crazyassmofo Linux - Software 0 07-27-2003 05:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration