title: How do I read data from an input file using ifstream correctly?
body: <p>I've already done multiple searches on this issue. I'm just getting back to programming and I'm trying to create a simple code just to get started.</p>
  
  <p>I'm currently using g++ to compile my files. I am using gedit to type my code and input file.</p>
  
  <p>Basically I want to read data from my input file (list of separate integers), find the total number of integers in the text file, then save them into an array.</p>
  
  <p>The following is a <strong>HEADER</strong> file to be used in my main file.</p>
  
  <pre><code>//Make program to read data from in_2.txt then add them and print to output file out_2.txt
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iomanip&gt;
  
  std::ofstream outputfile("out_2.txt", std::ios::out);
  
  class TEST_ADD{
      public:
          TEST_ADD(void); //constructor. Will use an unknown input file so nothing to do
  
          void INPUTREAD(char *); // Read the data from the input file. Ex.                       //b.INPUTREAD ("in.2")
  
          void ADD(void); //Will perform the actual summation and store value into int add
  
  
          void PRINT(void); // Print the numbers being summed, along with it's sum into out.2
  
      private:
          int add; //will store the sum of the numbers
          int n; //will keep track of total number of entries
          int A[20]; //used to store the number from the input file into an array
  
  };
  
  TEST_ADD::TEST_ADD (void)
  
  {
      n=0;
  }
  
  void TEST_ADD::INPUTREAD (char * in_name) //will record the users input file (written in main.cpp)
                         //and store it as in_name
  
  {
      int i;
      std::ifstream inputfile(in_name, std::ios::in);
      std::cout &lt;&lt; "Number of n before input file read : "&lt;&lt; n &lt;&lt; std::endl;
  
      inputfile &gt;&gt; n;
  
      /*while(!inputfile.eof()) 
      {
              inputfile &gt;&gt; n;
          //std::cout &lt;&lt; std::setw(10) &lt;&lt; n;
      }*/
  
      std::cout &lt;&lt; "Number of n after input file read : " &lt;&lt; n &lt;&lt; std::endl;
  
      std::cout &lt;&lt;  "There are ("&lt;&lt; n &lt;&lt; ") numbers in the input file" &lt;&lt; std::endl; //check
  
      for(i=0;i&lt;n;i++)
      {
          inputfile &gt;&gt; A[i];
          std::cout &lt;&lt; "A[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; A[i]&lt;&lt; std::endl;
      }
  
      outputfile &lt;&lt; "There are ("&lt;&lt; n &lt;&lt; ") numbers in the input file" &lt;&lt; std::endl;
  
  
  }
  </code></pre>
  
  <p><strong>Main File</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include "Project2.h"
  
  int main()
  {
      TEST_ADD b;
      b.INPUTREAD("in_2.txt");
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Input File</strong></p>
  
  <pre><code>1 
  2
  5
  9
  </code></pre>
  
  <p>The issue I think is the line</p>
  
  <pre><code>inputfile &gt;&gt; n;
  </code></pre>
  
  <p>This was the solution given to others, and this is what I used to use in class, but for some reason the text file isn't being read correctly so I must be doing something wrong. </p>
  
  <p>When I use the following, I set up a cout statement to test my code and this is what I got</p>
  
  <pre><code>jason@jason-Satellite-S55-B:~/C++/Second_program$ g++ main2.cpp -o project2
  main2.cpp: In function ‘int main()’:
  main2.cpp:10:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    b.INPUTREAD("in_2.txt");
                          ^
  jason@jason-Satellite-S55-B:~/C++/Second_program$ ./project2
  Number of n before input file read : 0
  Number of n after input file read : 1
  There are (1) numbers in the input file
  A[0] = 2
  </code></pre>
  
  <p>As you can see it is only counting <strong>1</strong> entry and then when I check to see what number it stored in the array, it stores <strong>2</strong> which is the second number in the text file. It just skipped the first entry completely.</p>
  
  <p>In case you were wondering, the actual out_2.txt file has</p>
  
  <pre><code>There are (1) numbers in the input file
  </code></pre>
  
  <p>I also tried the line</p>
  
  <pre><code>while(!inputfile.eof()) 
      {
              inputfile &gt;&gt; n;
          //std::cout &lt;&lt; std::setw(10) &lt;&lt; n;
      }
  </code></pre>
  
  <p>But that just brought up other problems where it counted <strong>9</strong> entries total, and the numbers stored in the array were large integers which aren't correct.</p>
  
  <p>Any help is appreciated. Thanks</p>