title: Checking data types in C++ using ifstream
body: <p>I'm trying to learn how to upload and read files using ifstream in C++ (fairly new to programming so I'm trying to start simple). As my example file, I have this in a text file:</p>
  
  <pre><code>3
  1
  2
  </code></pre>
  
  <p>and this in the main file:</p>
  
  <pre><code>int num;
  ifstream infile;
  infile.open(filename,ios::in);
  infile&gt;&gt;num;
  cout&lt;&lt;num&lt;&lt;endl;
  </code></pre>
  
  <p>so if I replace the first line in my text file:</p>
  
  <pre><code>k
  1
  2
  </code></pre>
  
  <p>I want my program to check that the first entry in my file is of type int and then exit out and give me an error if it is not. Instead, I always get 0 as the output. What can I do check for this possible error?</p>