title: C Programming- FindFirstFile returning Invalid_handle_value
body: <p>I've looked all over the place for the past couple of hours trying to find an answer for this, but i have a function meant to get the file size of one of my command line arguments, but in doing so my handle has constantly been returning an invalid value. If there's anyone willing to help me out it would be much appreciated. My function code is as follows:</p>
  
  <pre><code>int getSmallFileLength(const char *fileName, WIN32_FIND_DATA data)
  {
  HANDLE handle = FindFirstFile(&amp;fileName, &amp;data);
  int fileSize = data.nFileSizeLow;
  
  if (handle == INVALID_HANDLE_VALUE)
  {
      printf("\nyou got an error.");
      return -1;
  }
  else
  {
      printf("\nyour file size is %s", fileSize);
  }
  
  return fileSize;
  }
  </code></pre>
  
  <p>If the main code would help resolve this i will post it, but in the meanwhile I am cleaning up the mess that it is to make it readable. It is passing in my Win32_find_data variable and the file.</p>
  
  <p>I am also using Visual Studio 2013.</p>
  
  <p>and here was the main code now:</p>
  
  <pre><code>   #pragma warning (disable:4996)
     #include &lt;Windows.h&gt;
     #include &lt;stdio.h&gt;
     #include &lt;string.h&gt;
     #include &lt;stdlib.h&gt;
     #include "cA5_proto.h"
  
     int main(int argc, char *argv[])
     {
       int x;
       int argNum = 1;
       int counter = 0;
  
  printf("Command: %s\n", argv[0]);
  for (argNum = 1; argNum &lt; argc; argNum++)
  {
      printf("arg #%d: %s\n", argNum, argv[argNum]);
  }
  
  if (argc != 2) /* argc should be 2 for correct execution */
  {
      /* We print argv[0] assuming it is the program name */
      printf("usage: %s filename", argv[0]);
  }
  else
  {
  
      // We assume argv[1] is a filename to open
      FILE *file = fopen(argv[1], "rb");
      FILE *fileOutput = fopen("contents.txt", "a");
      WIN32_FIND_DATA fileData = { 0 };
      getSmallFileLength(file,fileData);
  
      /* fopen returns 0, the NULL pointer, on fnailure */
      if (file == 0)
      {
          printf("Could not open file\n");
      }
      else
      {
  
  
              /* read one character at a time from file, stopping at EOF, which
              indicates the end of the file.  Note that the idiom of "assign
              to a variable, check the value" used below works because
              the assignment statement evaluates to the value assigned. */
  
          //getSmallFileLength(argv[1]);              //getSmallFileLength outputs either the file size or an error
  
          printf("\nThe contents are: \n");
          while ((x = fgetc(file)) != EOF)
          {
                                                  //IN A FOR LOOP FOR THE LENGTH OF THE FILE SIZE HELPS OUTPUT A \N EVERY 10 CHARACTERS
  
              if (counter &gt; 0)
              {
                  if (counter == 10 || counter % 10 == 0)  //SEPERATE LINES WITH A \N EVERY TEN
                  {
                      fprintf(fileOutput, "\n");
  
                  }
              }
  
              if (x&gt;10 &amp;&amp; x&lt;100)                  //puts a zero in front of two digit numbers in the contents output file
              {
                  fprintf(fileOutput, "0");
              }
              fprintf(fileOutput, "%d ",x);
  
  
  
              printf("%c", x);
              counter++;
  
          }
          printf("\n");
  
          //CLOSE FILES HERE
          fprintf(fileOutput, "\n");              //PUT A \N AT THE END OF THE FILE
          fclose(fileOutput);
          fclose(file);
      }
  }
  return 0;
  
  }
  </code></pre>
  
  <p>The only warning i get from vsb is the following:Warning<br>
  1   warning C4133: 'function' : incompatible types - from 'FILE *' to 'const char *'    </p>