Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit:</strong> it's in fact <a href="http://boost-spirit.com/home/2010/01/05/stream-based-parsing-made-easy/" rel="nofollow noreferrer">possible to work on streams directly</a> with spirit than I suggested previously, with a parser:</p> <pre><code>+(omit[+(alpha|blank)] &gt;&gt; int_) </code></pre> <p>and one line of code(except for variable definitions):</p> <pre><code>void extract_file() { std::ifstream f("E:/dd/dd.trunk/sandbox/text.txt"); boost::spirit::istream_iterator it_begin(f), it_end; // extract all numbers into a vector std::vector&lt;int&gt; vi; parse(it_begin, it_end, +(omit[+(alpha|blank)] &gt;&gt; int_), vi); // print them to verify std::copy(vi.begin(), vi.end(), std::ostream_iterator&lt;int&gt;(std::cout, ", " )); } </code></pre> <p><strong><em>you get all numbers into a vector at once with one line, couldn't be simpler.</em></strong></p> <hr> <p>if you do not mind using <a href="http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/index.html" rel="nofollow noreferrer">boost.spirit2</a>. the parser to get numbers from a line only is</p> <pre><code>omit[+(alpha|blank)] &gt;&gt; int_ </code></pre> <p>to extract everything is</p> <pre><code>+(alpha|blank) &gt;&gt; int_ </code></pre> <p>See the whole program below(Test with VC10 Beta 2):</p> <pre><code>#include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;cstring&gt; #include &lt;vector&gt; #include &lt;fstream&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; using std::cout; using namespace boost::spirit; using namespace boost::spirit::qi; void extract_everything(std::string&amp; line) { std::string::iterator it_begin = line.begin(); std::string::iterator it_end = line.end(); std::string s; int i; parse(it_begin, it_end, +(alpha|blank)&gt;&gt;int_, s, i); cout &lt;&lt; "string " &lt;&lt; s &lt;&lt; "followed by nubmer " &lt;&lt; i &lt;&lt; std::endl; } void extract_number(std::string&amp; line) { std::string::iterator it_begin = line.begin(); std::string::iterator it_end = line.end(); int i; parse(it_begin, it_end, omit[+(alpha|blank)] &gt;&gt; int_, i); cout &lt;&lt; "number only: " &lt;&lt; i &lt;&lt; std::endl; } void extract_line() { std::ifstream f("E:/dd/dd.trunk/sandbox/text.txt"); std::string s; int i; // iterated file line by line while(getline(f, s)) { cout &lt;&lt; "parsing " &lt;&lt; s &lt;&lt; " yields:\n"; extract_number(s); // extract_everything(s); } } void extract_file() { std::ifstream f("E:/dd/dd.trunk/sandbox/text.txt"); boost::spirit::istream_iterator it_begin(f), it_end; // extract all numbers into a vector std::vector&lt;int&gt; vi; parse(it_begin, it_end, +(omit[+(alpha|blank)] &gt;&gt; int_), vi); // print them to verify std::copy(vi.begin(), vi.end(), std::ostream_iterator&lt;int&gt;(std::cout, ", " )); } int main(int argc, char * argv[]) { extract_line(); extract_file(); return 0; } </code></pre> <p>outputs:</p> <pre><code>parsing ALS 46000 yields: number only: 46000 string ALS followed by nubmer 46000 parsing BZK 39850 yields: number only: 39850 string BZK followed by nubmer 39850 parsing CAR 38000 yields: number only: 38000 string CAR followed by nubmer 38000 46000, 39850, 38000, </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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