1

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 Blade

Input => 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?

Jake Plaras
  • 43
  • 1
  • 4
  • 14

2 Answers2

4

You can use boost tokenizer for this:

#include <boost/tokenizer.hpp>
typedef boost::char_separator<char> separator_type;

boost::tokenizer<separator_type> tokenizer(my_text, separator_type(","));

auto it = tokenizer.begin();
while(it != tokenizer.end())
{
  std::cout << "token: " << *it++ << std::endl;
}

Also see getline to parse a line at a time from a file.

Gigi
  • 4,953
  • 24
  • 25
1
int main ()
{
    ifstream file("file.txt");
    string line;
    while (getline(file, line))
    {
        stringstream linestream(line);
        string item;
        while (getline(linestream, item, ','))
        {
            std::cout <<  item << endl;
        }
    }    
    return 0;
}
perreal
  • 94,503
  • 21
  • 155
  • 181