   Simple C in Linux
  I'm trying to list the filenames in a dir with linux and there seems to be a problem getting the value from d_type.#include <sys/stat.h>#include <stdio.h>#include <dirent.h>#include <unistd.h>int main(int argc, char **argv){    DIR *d;    struct dirent *de;    struct stat sb;    char tempType[100];    d = opendir(argv[1]);    if (d == NULL)    {printf("Your directory does not exist \n");return(1);    }    while((de = readdir(d)) != NULL)    {   tempType[0] = de->d_type;   printf("hello my type is, %s", tempType[0]); //problem here??   //printf("d_name is, %s  d_type is %s \n", de->d_name, de->d_type);    }    closedir(d);    return(0);}if I comment out the line, "printf("hello my type is, %s", tempType[0]); //problem here??" everything works fine. This must be a run-time error.error is[root@ptidds01 testCLinux]# ./a.out /DataSegmentation fault (core dumped)Any ideas why this is crashing my application?   
  tempType[0] is a char. The proper format code for that is %c, not %s.   ;Quote:Original post by SiCranetempType[0] is a char. The proper format code for that is %c, not %s.d_type is an unsigned char.d_name is a char.Why would d_name work with %s whereas, d_type fails with %s?EDIT: .. And it seems like using %c is just printing an empty string. Maybe I'm using the d_type in the wrong way. Are you able to printf from an unsigned char?Also note that I actually have my char arrays as temptype[1] and not temptype[0] like I posted above.   ;  d_name isn't a char, it's a char[].   ;  Why _would_ it, or why _did_ it?Did you type it in to find out if it fixes the problem?   ;  I know nothing about linux, but I can virtually gaurantee that d_type is probably going to be some type of identifier, and not be some type of symbolic representation, like you want it to be.You should probably look up in the API documentation to see how you should use d_type.EDIT:Well I did some dirty work for you: GNU C Library Documentation that explains the constants that can be used to manipulate d_type.   ;Quote:Original post by Halifax2I know nothing about linux, but I can virtually gaurantee that d_type is probably going to be some type of identifier, and not be some type of symbolic representation, like you want it to be.You should probably look up in the API documentation to see how you should use d_type.EDIT:Well I did some dirty work for you: GNU C Library Documentation that explains the constants that can be used to manipulate d_type.Thank You!!! I had no idea where to find this documentation. In linux you can doQuote:man 3 readdirThis method tells you the struct. But doesn't explain as much as the online documentation. Is this documentation not available in linux?   ;Quote:Original post by InstigatorQuote:man 3 readdirThis method tells you the struct. But doesn't explain as much as the online documentation. Is this documentation not available in linux?I'm guessing it's not available directly in linux, since that is GNU C documentation. Oh well, I wish you luck with the rest of your project.   