Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read from std::cin until the end of the stream?
    primarykey
    data
    text
    <p>My problem is, that I want to read the input from <code>std::cin</code> but don't know how long the input is. Also I have to <code>char</code> and can't use <code>std::string</code>. There are two ways I have to handle: a) The user inputs text and when he hits [ENTER] the program stops reading. b) The user redirects <code>std::cin</code> to a file (like <code>.\a.oput &lt; file</code>) which can hold multiple lines.</p> <p><strong>Edit:</strong> Just noticed that std::cin.eof() is always false also in the case of reading form a file.</p> <p>For a) I could read until \n occures. For b) <strong>Edit: No</strong> (I could read until std::cin.eof() occures.) But when I don't know whether I'm getting an a) or a b) problem, how can I break the reading process?</p> <p>This is what I have for a) ...</p> <pre><code>char buffer = ' '; while(std::cin &gt;&gt; std::noskipws &gt;&gt; buffer &amp;&amp; buffer != '\n') { // do some stuff with buffer } </code></pre> <p>... and b)</p> <pre><code>char buffer = ' '; while(std::cin &gt;&gt; std::noskipws &gt;&gt; buffer) { // do some stuff with buffer } </code></pre> <p>Also I know there is <code>std::cin.tellg()</code> to get the current position in the stream.</p> <p><strong>Edit:</strong> So it seems like in case of the file the input streams gets terminated, in the way that <code>std::cin &gt;&gt; std::noskipws &gt;&gt; buffer</code> gets <code>false</code>. What the code above does for a):</p> <ul> <li>It waits for the user to make an input and press [ENTER]</li> <li>Then it loops through every char entered by the user on the last line.</li> <li>Then it waits again for the user to make an input and press [ENTER]</li> <li>Infinite-waiting-processing-loop</li> </ul> <p>So how do I do it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload