Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a complete program that includes all the things you mentioned above I did change the way the file was read in slightly - I'm not familiar with the way you call getline() where the first parameter is the stream name, so I created a character buffer to read in the individual elements, then copy them to the pair. I also make sure I don't do something crazy at the end of the file, in case there are not two elements read (it works whether there's a \n at the end of file, or not).</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;list&gt; #define BUF 100 using namespace std; int main() { std::ifstream myReadFile; std::list&lt;std::pair&lt;std::string,std::string&gt; &gt; startTape; std::pair&lt;std::string,std::string&gt; pair; char sbuf[BUF]; // temp storage for file read myReadFile.open("listOwords.txt"); if(!myReadFile) { cerr &lt;&lt; "Error: file could not be opened" &lt;&lt; endl; exit(1); } cout &lt;&lt; "file opened successfully" &lt;&lt; endl; while(myReadFile.getline(sbuf, BUF, ',')) { pair.first = sbuf; myReadFile.getline(sbuf, BUF); pair.second = sbuf; if(myReadFile.good()) { // only process if both elements were read successfully // this deals with the problem of a "half pair" being read if the file is terminated with \n startTape.push_back(pair); cout &lt;&lt; "read a pair: " &lt;&lt; pair.first &lt;&lt; ", " &lt;&lt; pair.second &lt;&lt; endl; } } myReadFile.close(); cout &lt;&lt; "Size of startTape is now " &lt;&lt; startTape.size() &lt;&lt; endl; std::pair&lt;std::string,std::string&gt; firstCompare = startTape.front(); startTape.remove(*startTape.begin()); cout &lt;&lt; "Size of startTape is now " &lt;&lt; startTape.size() &lt;&lt; endl; std::pair&lt;std::string,std::string&gt; secondCompare = startTape.front(); startTape.remove(*startTape.begin()); cout &lt;&lt; "Size of startTape is now " &lt;&lt; startTape.size() &lt;&lt; endl; exit(0); } </code></pre> <p>The contents of listOwords:</p> <pre><code>&gt;cat listOwords.txt N, C I, G A, U H, A G, M C, I S, H U, N </code></pre> <p>The output I get from this is:</p> <pre><code>file opened successfully read a pair: N, C read a pair: I, G read a pair: A, U read a pair: H, A read a pair: G, M read a pair: C, I read a pair: S, H read a pair: U, N Size of startTape is now 8 Size of startTape is now 7 Size of startTape is now 6 </code></pre> <p>Let me know if you don't get the same results with this exact code?</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. 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