Note that there are some explanatory texts on larger screens.

plurals
  1. POReading parts of an input file
    primarykey
    data
    text
    <p>I would like to read an input file in C++, for which the structure (or lack of) would be something like a series of lines with <em>text = number</em>, such as</p> <pre><code>input1 = 10 input2 = 4 set1 = 1.2 set2 = 1.e3 </code></pre> <p>I want to get the number out of the line, and throw the rest away. Numbers can be either integers or doubles, but I know when they are one or other.</p> <p>I also would like to read it such as</p> <pre><code>input1 = 10 input2=4 set1 =1.2 set2= 1.e3 </code></pre> <p>so as to be more robust to the user. I think this means that it shouldn't be red in a formatted fashion.</p> <p>Anyway, is there a smart way to do that?</p> <p>I have already tried the following, but with minimal knowledge of what I've been doing, so the result was as expected... no success.</p> <pre><code> #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;float.h&gt; #include &lt;math.h&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;iomanip&gt; #include &lt;cstdlib&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;string&gt; using namespace std; using namespace boost; int main(){ string tmp; char temp[100]; int i,j,k; ifstream InFile("input.dat"); //strtol InFile.getline(temp,100); k=strtol(temp,0,10); cout &lt;&lt; k &lt;&lt; endl; //lexical_cast InFile.getline(temp,100); j = lexical_cast&lt;int&gt;(temp); cout &lt;&lt; j &lt;&lt; endl; //Direct read InFile &gt;&gt; tmp &gt;&gt; i; cout &lt;&lt; i &lt;&lt; endl; return 0; } </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.
 

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