Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get an EXC_BAD_ACCESS here and how can I make it work?
    primarykey
    data
    text
    <p>I'm trying to make my code be able to separate <a href="http://drop.io/xodrl0f" rel="nofollow noreferrer">a file</a> into a customer database (it's delimited by many spaces and not tabs). I try to use strtok, but I get an EXC_BAD_ACCESS error. Here is my main.cpp code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include "Cust.h" using namespace std; int main (int argc, char * const argv[]) { Cust customers[500]; int idx = 0; string tmpString = ""; string tmpAcctFN = ""; string tmpAcctLN = ""; ifstream input("P3_custData.txt"); while (!input.eof()){ getline(input,tmpString); tmpString.insert(0,""); customers[idx].setAcctNum(atoi(strtok((char *)tmpString.c_str()," "))); customers[idx].setAcctFN(strtok(NULL," ")); customers[idx].setAcctLN(strtok(NULL," ")); //customers[idx].setCurrBalance(atof(strtok((char *) tmpString.c_str()," "))); } cout &lt;&lt; "return 0;"; return 0; } </code></pre> <p>I still get an EXC_BAD_ACCESS after making changes based on the comments:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;sstream&gt; #include "Cust.h" using namespace std; int main (int argc, char * const argv[]) { Cust customers[500]; int idx = 0; string tmpString = ""; string tmpAcctFN = ""; string tmpAcctLN = ""; char * s; ifstream input("P3_custData.txt"); while (!input.eof()){ getline(input,tmpString); s = strdup (tmpString.c_str()); customers[idx].setAcctNum(atoi(strtok(s," "))); customers[idx].setAcctFN(strtok(NULL," ")); customers[idx].setAcctLN(strtok(NULL," ")); //customers[idx].setCurrBalance(atof(strtok((char *) tmpString.c_str()," "))); } cout &lt;&lt; "return 0;"; 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.
    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