title: How to read data from a text file and store it in a variable in C language?
body: <p>I am trying to read a text file in C. The name of the file is <strong>test.txt</strong> and has the following kind of format.</p>
  
  <pre><code>Nx = 2
  Ny = 4
  T  = 10
  </code></pre>
  
  <p>I have written this C code to read the values of Nx, Ny, and T which is 2, 4, and 10 respectively.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void main()
  {
      double Data[3];    // I'm interested in this information
      char junk1, junk2; // junk variables to avoid first two characters
  
      FILE * file = fopen("test.txt", "r"); // open file
  
      for(int i = 0; i &lt; 3; i++) // each loop will read new line of file; i&lt;3 for 3 lines in file
      {
          fscanf(file, "%s %s %lf\n", &amp;junk1, &amp;junk2, &amp;Data[i]); //store info in Data array
          printf("%f\n", Data[i]); // print Data, just to check
      }
      fclose(file);
  
      int Nx; // store data in respective variables
      int Ny;
      double T;
  
      Nx = Data[0];
      Ny = Data[1];
      T  = Data[2];
  
      printf("Value of Nx is %d\n", Nx); // Print values to check
      printf("Value of Ny is %d\n", Ny);
      printf("Value of T is %f\n", T);
  }
  </code></pre>
  
  <p>But got this as an output. This output is wrong as the values of Nx, Ny, and T are not matching with the data given above.</p>
  
  <p><a href="https://i.stack.imgur.com/3kDOo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3kDOo.png" alt="Code output"></a></p>
  
  <p>Please help me to solve this problem.</p>