Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My guess is that your test.dat file is not in the working directory. To verify that you can test the stream:</p> <pre><code>if( ! theStream ) std::cerr &lt;&lt; "Error opening file test.dat\n"; </code></pre> <p>Check your project properties in XCode to see what the working directory is, and place the file there.</p> <p>If your file contains data that doesn't parse as integers then the streams' <code>int</code> parsing will fail. Make sure the line you read has nothing but integers and whitespace:</p> <pre><code>#include &lt;cctype&gt; // algorithm iterator ... struct not_digit_and_not_whitespace { bool operator()(char c) const { return ! std::isdigit(c) &amp;&amp; ! std::isspace(c); } }; // ... line.erase(remove_if( line.begin(), line.end(), not_digit_and_not_whitespace()), line.end() ); </code></pre> <p>Also, you can simplify your code. First, if you include <code>&lt;iostream&gt;</code> then you don't need <code>&lt;istream&gt;</code>. Secondly, you can use the range constructor of <code>std::vector</code> to get the integers.</p> <pre><code>#include &lt;algorithm&gt; // iterator fstream iostream vector string sstream int main() { std::string line; std::ifstream theStream("test.dat"); std::getline(theStream, line); line.erase(std::remove(line.begin(), line.end(), '\'' ), line.end()); std::istringstream myStream(line); std::vector&lt;int&gt; numbers((std::istream_iterator&lt;int&gt;(myStream)), std::istream_iterator&lt;int&gt;()); std::copy(numbers.begin(), numbers.end(), std::ostream_iterator&lt;int&gt;(std::cout, "\n")); return 0; } </code></pre>
 

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