title: system calls read and write in C
body: <p>I am wondering how to use the system calls read() and write() in C. 
  I am trying to read in the contents of a pre existing, file within a directory, into a buffer (array) so I can step through the array and determine what type of file was read. I have looked at quite a few different posts on the matter and have not been able to figure out where I am going wrong. I am trying to print out my buffer array at the bottom to make sure it holds the correct contents of a file before stepping though it to determine the file type, but the buffer holds nothing. Any help would be greatly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/unistd.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;time.h&gt;
  
  int main(int argc, char *argv[])
  {
      char *currentDir = NULL;
      DIR *myDir = NULL;
      struct dirent *myFile = NULL;
      struct stat myStat;
  
      const void *buf [1024];
  
      int count;
      int currentFile;
  
      if (strcmp(argv[1], "ls") == 0 &amp;&amp; argc &lt; 3)
      {
          currentDir = getenv("PWD");
          myDir = opendir(currentDir);
  
          while ((myFile = readdir(myDir)) != NULL)
          {
              if (myFile-&gt;d_name[0] != '.')
              {
                  puts(myFile-&gt;d_name);
                  //printf("%s\n", myFile-&gt;d_name);
              }
          }
          closedir(myDir);
      }
  
          if (strcmp(argv[1], "ls") == 0 &amp;&amp; strcmp(argv[2], "-t") == 0)
          {
              currentDir = getenv("PWD");
              myDir = opendir(currentDir);
  
              while ((myFile = readdir(myDir)) != NULL)
              {
                  if (myFile-&gt;d_name[0] != '.')
                  {
                      printf("%s\n", myFile-&gt;d_name);
                      stat (myFile-&gt;d_name, &amp;myStat);
                      printf("Last Accessed:\t%s\n", ctime(&amp;myStat.st_atime));
                      printf("Last Modified:\t%s\n", ctime(&amp;myStat.st_mtime));
                      printf("Last Changed:\t%s\n", ctime(&amp;myStat.st_ctime));
                  }
              }
              closedir(myDir);
          }
  
              if (strcmp(argv[1], "ls") == 0 &amp;&amp; strcmp(argv[2], "-f") == 0)
              {
                  currentDir = getenv("PWD");
                  myDir = opendir(currentDir);
  
                  while ((myFile = readdir(myDir)) != NULL)
                  {
                      //while (count = read(0, buf, 100) &gt; 0)
                      //{
  
                      //}
                      //write (1, buf, 100);
                      //printf ("Buffer Holds:\n %s\n", buf);
                      if (myFile-&gt;d_name[0] != '.')
                      {
                          while (count = read(myFile-&gt;d_name, buf, 100) &gt; 0)
                              write (1, buf, count);
  
                          printf ("Buffer Holds:\n %s\n", buf);
                      }
                  }
              }
      return 0;
  }
  </code></pre>