title: Print out file names and its' sizes in C
body: <p>I'm not sure if C can do this, but I'm hoping that I can make a program that will look into a directory, and print out all of the contents of the directory along with the file size of each file. As in I wanted it to look like this (possibly):</p>
  <blockquote>
  <p>filename.txt   -- 300 bytes</p>
  <p>filename2.txt  -- 400 bytes</p>
  <p>filename3.txt  -- 500 bytes</p>
  </blockquote>
  <p>And so on.</p>
  <p>So far, I created a program that can open a file, and it will print the bytes, but it does not read the entire directory, and I have to be specific with which file I want to read.. (which is not what I want).</p>
  <p>Here is what I have so far:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(){
      FILE *fp; // file pointer
      long fileSize;
      int size;
  
      // opens specified file and reads
      fp = fopen( &quot;importantcommands.txt&quot;, &quot;rw&quot; );
      
      if( fp == NULL ){
          printf( &quot;Opening file error\n&quot; );
          return 0;
      }
  
      // uses fileLength function and prints here
      size = fileLength(fp);
      printf( &quot;\n Size of file: %d bytes&quot;, size );
  
      fclose(fp);
  
      return 0;
  }
  
  int fileLength( FILE *f ){
      int pos;
      int end;
  
      // seeks the beginning of the file to the end and counts
      // it and returns into variable end
      pos = ftell(f);
      fseek (f, 0, SEEK_END);
      end = ftell(f);
      fseek (f, pos, SEEK_SET);
  
      return end;
  }
  </code></pre>
  <p>Please help.</p>