title: Read numbers from a text file in C#
body: <p>This is something that should be very simple. I just want to read numbers and words from a text file that consists of tokens separated by white space. How do you do this in C#? For example, in C++, the following code would work to read an integer, float, and word. I don't want to have to use a regex or write any special parsing code.</p>
  
  <pre><code>ifstream in("file.txt");
  int int_val;
  float float_val;
  string string_val;
  in &gt;&gt; int_val &gt;&gt; float_val &gt;&gt; string_val;
  in.close();
  </code></pre>
  
  <p>Also, whenever a token is read, no more than one character beyond the token should be read in. This allows further file reading to depend on the value of the token that was read. As a concrete example, consider</p>
  
  <pre><code>string decider;
  int size;
  string name;
  
  in &gt;&gt; decider;
  if (decider == "name")
      in &gt;&gt; name;
  else if (decider == "size")
      in &gt;&gt; size;
  else if (!decider.empty() &amp;&amp; decider[0] == '#')
      read_remainder_of_line(in);
  </code></pre>
  
  <p>Parsing a binary PNM file is also a good example of why you would like to stop reading a file as soon as a full token is read in.</p>