title: Reading constant number of characters in C++
body: <p>How would you read a string if it was no longer than 20 characters and could contain spaces in C++? For example I have this kind of file:</p>
  
  <pre><code>3
  Namama               4 5 12
  Cool                 4 2 34
  Not So Cool          4 2 45
  </code></pre>
  
  <p>I want to write it in the most basic way possible. And I would prefer using only C++ (no C thingies) in this case. This means I want to store it in std::string.</p>
  
  <p>I could use <em>istream::get</em> or <em>istream::getline</em> but then I have to avoid the new line characters. This is problematic (and the code I'm writing is going to be shown to beginners). </p>
  
  <p>What are your solutions for this problem?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>As asked I'm going to tell you what I have tried but I don't think it's going to be any good. I am not a C++ beginner and usually I would have used something like <em>istream::get</em> and char array but removing new line characters might seem too techy for some people.</p>
  
  <p>So... Either reading char array or <em>std::string</em> using <em>istream::operator>></em> fails because it stops reading when they see space character (and I may have to read several words). This means the following code fails:</p>
  
  <pre><code>char name[21];
  std::cin &gt;&gt; name;
  </code></pre>
  
  <p>or...</p>
  
  <pre><code>std::string name;
  std::cin &gt;&gt; name;
  </code></pre>
  
  <p>Another thing is that new line characters differ from system to system and actually Windows use two of them by default and I have tried using <em>istream::ignore</em> with 2 as an argument on Windows and that was the only way to ignore the new line and I came to conclusion that it's because Windows use two characters for a new line mark. That means it wouldn't work on Linux and it would have to be more complex... Again - bad for beginners.</p>