Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.20 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. read file and tokenize data to compare to input in C  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. main () {
  8. //  string toks[];
  9.   char oneline[80],*del;
  10.   string line, creds[3], username, password;
  11.   int x = 0;
  12.   cout<<"Enter Username: ";
  13.   cin>>username;
  14.   cout<<"Enter Password: ";
  15.   cin>>password;
  16.   ifstream myfile;
  17.    myfile.open("jake.txt");
  18.   if (myfile.is_open())
  19.   {
  20.  
  21.     while (!myfile.eof())
  22.     {
  23.      getline(myfile,line);
  24.      strcpy(oneline,line.c_str());
  25.      del = strtok(oneline,",");
  26.      while(del!=NULL)
  27.      {
  28.      creds[x] = del;
  29.      del = strtok(NULL,",");
  30.      ++x;
  31.      }
  32.      if((creds[0]==username)&&(creds[1]==password))
  33.         {
  34.          cout<<creds[2]<<endl;
  35.          break;
  36.          }
  37.     }
  38.     myfile.close();
  39.   }
  40.   else
  41.   cout << "Unable to open file";
  42.  
  43.   system("pause");
  44. }
  45.        
  46. string oneline;
  47. ...
  48. oneline = line;
  49. ...
  50.        
  51. #include<iostream>
  52. #include<boost/tokenizer.hpp>
  53. #include<string>
  54.  
  55. int tokenizer_main(int, char **){
  56.    using namespace std;
  57.    using namespace boost;
  58.    string s = "This,is,a,test";
  59.    tokenizer<> tok(s);
  60.    for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
  61.        cout << *beg << "n";
  62.    }
  63. }
  64.        
  65. This
  66. is
  67. a
  68. test