I'm trying to make my code be able to separate a file into a customer database (it's delimited by many spaces and not tabs). I try to use strtok, but I get an EXC_BAD_ACCESS error. Here is my main.cpp code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Cust.h"
using namespace std;
int main (int argc, char * const argv[]) {
Cust customers[500];
int idx = 0;
string tmpString = "";
string tmpAcctFN = "";
string tmpAcctLN = "";
ifstream input("P3_custData.txt");
while (!input.eof()){
getline(input,tmpString);
tmpString.insert(0,"");
customers[idx].setAcctNum(atoi(strtok((char *)tmpString.c_str()," ")));
customers[idx].setAcctFN(strtok(NULL," "));
customers[idx].setAcctLN(strtok(NULL," "));
//customers[idx].setCurrBalance(atof(strtok((char *) tmpString.c_str()," ")));
}
cout << "return 0;";
return 0;
}
I still get an EXC_BAD_ACCESS after making changes based on the comments:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Cust.h"
using namespace std;
int main (int argc, char * const argv[]) {
Cust customers[500];
int idx = 0;
string tmpString = "";
string tmpAcctFN = "";
string tmpAcctLN = "";
char * s;
ifstream input("P3_custData.txt");
while (!input.eof()){
getline(input,tmpString);
s = strdup (tmpString.c_str());
customers[idx].setAcctNum(atoi(strtok(s," ")));
customers[idx].setAcctFN(strtok(NULL," "));
customers[idx].setAcctLN(strtok(NULL," "));
//customers[idx].setCurrBalance(atof(strtok((char *) tmpString.c_str()," ")));
}
cout << "return 0;";
return 0;
}