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.
For example: Data from file =>
andrew,andy,Andrew Anderson
jade,jaded,Jade Sonia BladeInput => jade
Output => jaded
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?
EDIT
regarding this problem i think i am getting somewhere... but still the output screen crashes when i run it. this is my code
#include <iostream>
#include <fstream>
#include <string>
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 << "Unable to open file";
system("pause");
}
can anyone shed light on this for me please?
EDIT ....
I have some progress on this one... the problem now is that when the input is matched with the next line, it crashes...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
main () {
// string toks[];
char oneline[80],*del;
string line, creds[3], username, password;
int x = 0;
cout<<"Enter Username: ";
cin>>username;
cout<<"Enter Password: ";
cin>>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)&&(creds[1]==password))
{
cout<<creds[2]<<endl;
break;
}
}
myfile.close();
}
else
cout << "Unable to open file";
system("pause");
}
can someone help me with this please?