title: Reading integers with fread in ANSI C
body: <p>I am writing a program in C, and I am reading a text file using <code>fread</code> and I cannot read correctly integers. </p>
  
  <p>The value of <code>a</code> in the text file is 16, the output I get is:</p>
  
  <pre><code> a= 538976288
  </code></pre>
  
  <p>I have used an analogous code that read successfully binary files. I would like to learn what is the best way to read ASCII tables in c.</p>
  
  <p>The table size is given by the integers on the top of the file. The data are separated by spaces.</p>
  
  <p>This is the simple code I wrote:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char *argv[])
  {
    int a, flag_fread;
    FILE * p_finput = NULL;
    char * filename = NULL;
  
    filename = malloc(sizeof(char) * (strlen(argv[1]) + 1));
    strncpy(filename, argv[1],strlen(argv[1]));
    filename[strlen(argv[1])] = '\0';
  
    p_finput = fopen(filename, "r");
    if (p_finput == NULL)
      {
        printf("couldn't open %s \n", filename);
        abort();
      }
  
  
    flag_fread = fread(&amp;(a), sizeof(int), 1, p_finput);
  
    printf("a= %d \n", a);  
  
    fclose(p_finput);
    free(filename);
    return 0;
  }
  </code></pre>