Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ read from file and tokenize data
    primarykey
    data
    text
    <p>I'm trying to create a C++ program that allows me to read from a file and find a match of an input from every line. Note that every line is a single record delimited by a coma. If a match has been found, the expected output will be a string from a record. </p> <blockquote> <p>For example: Data from file => </p> <p>andrew,andy,Andrew Anderson <br> jade,jaded,Jade Sonia Blade</p> <p>Input => jade</p> <p>Output => jaded</p> </blockquote> <p>How can I do this? I'm trying to implement strtok, but to no avail. So far I'm getting no good results. Can someone please help me with this?</p> <p>EDIT</p> <p>regarding this problem i think i am getting somewhere... but still the output screen crashes when i run it. this is my code</p> <pre><code> #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; main () { // string toks[]; char oneline[80],*del; string line, creds[4]; int x = 0; ifstream myfile; myfile.open("jake.txt"); if (myfile.is_open()) { while (!myfile.eof()) { getline(myfile,line); strcpy(oneline,line.c_str()); del = strtok(oneline,","); while(del!=NULL) { creds[x] = del; del = strtok(NULL,","); x++; } } myfile.close(); } else cout &lt;&lt; "Unable to open file"; system("pause"); } </code></pre> <p>can anyone shed light on this for me please?</p> <p>EDIT ....</p> <p>I have some progress on this one... the problem now is that when the input is matched with the next line, it crashes... </p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; main () { // string toks[]; char oneline[80],*del; string line, creds[3], username, password; int x = 0; cout&lt;&lt;"Enter Username: "; cin&gt;&gt;username; cout&lt;&lt;"Enter Password: "; cin&gt;&gt;password; ifstream myfile; myfile.open("jake.txt"); if (myfile.is_open()) { while (!myfile.eof()) { getline(myfile,line); strcpy(oneline,line.c_str()); del = strtok(oneline,","); while(del!=NULL) { creds[x] = del; del = strtok(NULL,","); ++x; } if((creds[0]==username)&amp;&amp;(creds[1]==password)) { cout&lt;&lt;creds[2]&lt;&lt;endl; break; } } myfile.close(); } else cout &lt;&lt; "Unable to open file"; system("pause"); } </code></pre> <p>can someone help me with this please?</p>
    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.
 

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