title: C++ read from file and tokenize data
body: <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>