Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple .txt File-Reading with ifstream
    primarykey
    data
    text
    <p>Just what is probably a basic question for something I haven't been able to figure out all day. Here's a snippet of code in C++:</p> <pre><code>Escape::Escape(std::string filename) { std::ifstream file; file.open(filename.c_str()); if (file.is_open()) { file &gt;&gt; gridWidth &gt;&gt; gridHeight &gt;&gt; nKeys; std::cout &lt;&lt; "gridWidth = " &lt;&lt; gridWidth &lt;&lt; std::endl; std::cout &lt;&lt; "gridHeight = " &lt;&lt; gridHeight &lt;&lt; std::endl; std::cout &lt;&lt; "nKeys = " &lt;&lt; nKeys &lt;&lt; std::endl; /* irrelevant code below */ </code></pre> <p>Basically I want this code to read the first three integers of a text file and save them into the variables gridWidth, gridHeight, and nKeys. These are private integers of a class called "Escape". Here are the first few lines of the text file:</p> <pre><code>8 8 1 BUL--/EUR--/BUL--/BU---/BU---/BU---/BU---/BUR--/ (more text below) </code></pre> <p>And here is some sample output of this code:</p> <pre><code>gridWidth = 107764358 gridHeight = -991553646 nKeys = 0 </code></pre> <p>On running this code multiple times, <code>gridWidth</code> and <code>gridHeight</code> are always junk and nKeys is always 0. Any ideas as to what I am doing wrong?</p> <p><strong>EDIT: The problem was with the filename I passed to <code>file.open()</code>. I passed in the relative filename which, although openable, was not linkable to the ifstream. The problem was resolved by using the text file's absolute filename instead.</strong></p>
    singulars
    1. This table or related slice is empty.
    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.
    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