can you help me with this.. what i’m trying to accomplish is that when the user inputs his/her username and password, my program will then open a file which is the storage of the users information arranged by line (username,password,foldername,user’s full name). I have this code but i can’t seem to see the problem. When the input should have the result of the first line, it gets it. But when it parses the second line, it crashes. Can someone help me with this?
#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");
}
probably you are overflowing the oneline buffer.
Instead of using a fixed size, just use std::string, as you are doing for other vars.
i.e.
and use boost tokenizer for split on comma, instead of strtok
edit:sample usage, adapted from boost docs
outputs