title: Why won't my S_ISDIR and S_ISREG return the correct directory/file name?
body: <p>Why won't my S_ISDIR and S_ISREG return the correct directory/file name?</p>
  
  <p>When I run my program using a test directory that I made with 3 other directories and a text file inside, only the printf() for Dir executes.</p>
  
  <pre><code>#include "dirAssignment.h"
  
  //Struct
  typedef struct directories{
  
  }directories;
  
  
  //Method to look for directories and files:
  void search(const char *fileName){
      struct stat dirInfo;
      struct dirent *dirp;
      DIR *dp;
  
      //Checks to see if there is an error with the fileName:
      if (lstat(fileName, &amp;dirInfo) &lt; 0)
          {
          //Error:
          printf("Something went wrong!\n");
          printf("%s\n",strerror(errno));
      }
  
      //Checks to see if file can be opened:
      if ((dp = opendir(fileName)) == NULL)
      {
          printf("Cannot open file %s\n", fileName);
          printf("%s\n", strerror(errno));
      }
  
      //If file can be opened, get all the files from the directory:
      while((dirp = readdir(dp)) != NULL)
      {   
          //Check for( and Ignore) parent and self:
          if (strcmp(dirp-&gt;d_name,".") == 0 || strcmp(dirp-&gt;d_name,"..") == 0)        
          {
              continue;
          }
          printf("%s:\t",dirp-&gt;d_name);
  
  
          if(S_ISDIR(dirInfo.st_mode))
          {
              printf("Dir\n");
          }
          else if(S_ISREG(dirInfo.st_mode))
          {
              printf("File\n");
          }
      /*
          else if(S_ISDIR(dirp-&gt;d_name, &amp;dirInfo))
          {
              //stat(fileName, &amp;dirInfo);
  
              //Checks to see if the file is a directory:
              if (S_ISDIR(getcwd(dirp-&gt;d_name,512).st_mode))
              {
                  printf("directory in: %s\n", fileName);
              }
              //Must be a file:
              else if (S_ISREG(getcwd(dirp-&gt;d_name,512).st_mode))
              {
                  printf("file in: %s\n", fileName);
              }
              //No clue how you got here!
              else
              {
                  printf("Danger Will Robinson, Danger... Danger!!!\n");
              }
          }
          else
          {
              printf("Danger Will Robinson, Danger... Danger!!!\n");
          }
      */
  
       }//End of while loop
  }//End of Search method
  
  int main(int argc, char const *argv[])
  {
      //Variables go here:
      int i = 0;
      char const *fileName = argv[1];
  
  
      //Prompt the user:
      printf("Enter starting fileName: %s\n",argv[1]);
      //Starting file:
      printf("Starting from file: %s\n...\n\n\n\n", fileName);
  
      //Lettuce(lol) find some files and directories!
      search(fileName);
  
  
      return 0;
  }//End of main method
  </code></pre>