title: Same .txt files, different sizes?
body: <p>I have a program that reads from a .txt file</p>
  
  <p>I use the cmd prompt to execute the program with the name of the text file to read from.</p>
  
  <p>ex: program.exe myfile.txt</p>
  
  <p>The problem is that sometimes it works, sometimes it doesn't. </p>
  
  <p>The original file is 130KB and doesn't work. 
  If I copy/paste the contents, the file is 65KB and works.
  If I copy/paste the file and rename it, it's 130KB and doesn't work.</p>
  
  <p>Any ideas?</p>
  
  <p>After more testing it shows that this is what makes it not work:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      char *infile1
      char tmp[1024] = { 0x0 };
      FILE *in;
      for (i = 1; i &lt; argc; i++)  /* Skip argv[0] (program name). */
      {
          if (strcmp(argv[i], "-sec") == 0)  /* Process optional arguments. */
          {
              opt = 1;  /* This is used as a boolean value. */
  
              /*
              * The last argument is argv[argc-1].  Make sure there are
              * enough arguments.
              */
  
              if (i + 1 &lt;= argc - 1)  /* There are enough arguments in argv. */
              {
                  /*
                  * Increment 'i' twice so that you don't check these
                  * arguments the next time through the loop.
                  */
  
                  i++;
                  optarg1 = atoi(argv[i]);  /* Convert string to int. */
  
              }
          }
          else /* not -sec */
          {
              if (infile1 == NULL) {
                  infile1 = argv[i];
              }
              else {
                  if (outfile == NULL) {
                      outfile = argv[i];
                  }
              }
          }
       }
  
       in = fopen(infile1, "r");    
  
       if (in == NULL) 
       {
             fprintf(stderr, "Unable to open file %s: %s\n", infile1, strerror(errno));
             exit(1);
       }
  
       while (fgets(tmp, sizeof(tmp), in) != 0)
       {
           fprintf(stderr, "string is %s.", tmp);
           //Rest of code
       }
  }
  </code></pre>
  
  <p>Whether it works or not, the code inside the while loop gets executed.</p>
  
  <p>When it works tmp actually has a value.
  When it doesn't work tmp has no value.</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Thanks to sneftel, we know what the problem is,
  For me to use fgetws() instead of fgets(), I need tmp to be a wchar_t* instead of a char*.
  Type casting seems to not work. 
  I tried changing the declaration of tmp to
      wchar_t tmp[1024] = { 0x0 };
  but I realized that tmp is a parameter in strtok() used elsewhere in my code.
  I here is what I tried in that function:</p>
  
  <pre><code>//tmp is passed as the first parameter in parse()
  void parse(wchar_t *record, char *delim, char arr[][MAXFLDSIZE], int *fldcnt)
  {
      if (*record != NULL)
      {
          char*p = strtok((char*)record, delim);
          int fld = 0;
          while (p) {
              strcpy(arr[fld], p);
              fld++;
              p = strtok('\0', delim);
          }
          *fldcnt = fld;
      }
      else
      {
          fprintf(stderr, "string is null");
      }
  }
  </code></pre>
  
  <p>But typecasting to char* in strtok doesn't work either.</p>
  
  <p>Now I'm looking for a way to just convert the file from UTF-16 to UTF-8 so tmp can be of type char*
  I found this which looks like it can be useful but in the example it uses input from the user as UTF-16, how can that input be taken from the file instead?
  <a href="http://www.cplusplus.com/reference/locale/codecvt/out/" rel="nofollow">http://www.cplusplus.com/reference/locale/codecvt/out/</a></p>