Question with directory listing into array

*edit

1
2
3
4
5
6
7
8
9
10
11
12
13
char index[100];

struct dirent *dp;
	DIR *dfd = opendir(".");
	if(dfd != NULL) {
		while((dp = readdir(dfd)) != NULL)
		{
			printf("%s\n", (*dp).d_name);
			index=(*dp).d_name)
		}
		closedir(dfd);
	}



How can assign dp->d_name to index properly?

index=(*dp).d_name) isn't working.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   //Suspect - should be a two dimensional array and initialised to all zeros
    char index[100]; 
    
    int i=0;

    struct dirent *dp;
	DIR *dfd = opendir(".");

	if(dfd != NULL) //Loop check - what happens if directory contains more than 100 files?
    {
		while((dp = readdir(dfd)) != NULL)
		{
			printf("%s\n", (*dp).d_name);
			
            //As it stands - All this would do is copy the first char of the filename.
            //should be a string copy to a char array.
            index[i]=(char)(*dp).d_name;

			i++;
		}
		closedir(dfd);
	}
		
    //This next line shouldn't compile - no index has been specified
    //But I suppose it depends on the compiler.
    //Probably why it crashed.
    //should be a loop.
    printf("\n%s",index[]); 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

int main()
{
	FILE *fr;
	int depth,l,w,q;
	char material[100],code[10];
	
	char index[100];
	char directory[]="c:\\ALA\\in\\";
	char file[25];
	char str[50];
			
	struct dirent *dp;
	DIR *dfd = opendir(directory);
	if(dfd != NULL) 
	{
		while((dp = readdir(dfd)) != NULL)
		{
			strcpy(str,directory);
			strcat(str,file);
			//printf("%s\n",str);
			
			fr=fopen(str,"r");
					
			fscanf(fr,"%[^;];%d;%d;%d;%d;%s",&material,&depth,&l,&w,&q,&code);
			printf("%s %d %d %d %d %s\n",material,depth,l,w,q,code);
								
			//printf("%s\n", dp->d_name);
									
		}
		closedir(dfd);
	}		
	
	fclose(fr);
	fclose(fw);
	return 0;
	}


Ok. Here's the whole program.
I need to open read all the files in the directory as many as they are...and print them. So I need to pass directory+file name to fopen. What I don't know is how to set file=(*dp).d_name)(which obviously isn't going to work).
Last edited on
OK by using strcpy(file,dp->d_name); I'm passing the file name to file.
Now I need to make the program read only the files in the directory and without reading the folders.

The d_type field isn't availiable for some reason...but I need to extract files from the directory.

http://www.delorie.com/gnu/docs/glibc/libc_270.html
See if your compiler supports the stat() function/system call.
It does...i use Minw Studio and/or Dev C++ on Windows XP.

I have read about stat before but it seems to be a little more difficult to understand than dirent.
Yes, but if dirent doesn't do what you need, then all you need to do is

1
2
3
4
5
6
7
8
9
10
struct stat statStruct;
if( stat( fileName, &statStruct ) ) < 0 )
    /* bomb out */;
if( S_ISDIR( statStruct.st_mode ) )
   /* ignore dirs */;

// or

if( S_ISREG( statStruct.st_mode ) )
   /* regular file */;


for each entry returned by dirent.
What about this?
Put your own directory name in - I just used test because it was more convinient for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
	FILE *fr;
	int depth,l,w,q;
	char material[100],code[10];
	
	char index[100]; //Not used at this stage - redundant?
	char directory[]=".\\test\\";
	char file[25];//not used - redundant?
	char str[50]; //maybe make this larger
			
	struct dirent *dp;
	DIR *dfd = opendir(directory);
	if(dfd != NULL) 
	{
		while((dp = readdir(dfd)) != NULL) 
		{
			      
            strcpy(str,directory);

            //Don't bother with the . or .. filenames
            if(strcmpi(dp->d_name,".") == 0 || strcmpi(dp->d_name,"..")==0)
                continue;

            strcat(str,dp->d_name);
			printf("%s\n",str);
			
			fr=fopen(str,"r");
					
			fscanf(fr,"%[^;];%d;%d;%d;%d;%s",&material,&depth,&l,&w,&q,&code);
			printf("%s %d %d %d %d %s\n",material,depth,l,w,q,code);
								
			//printf("%s\n", dp->d_name);
									
		}
		closedir(dfd);
	}		
	
	fclose(fr);
Topic archived. No new replies allowed.