I am trying to create a function, readBooks, that opens an input file stream, reads a list of books and authors separated by a comma with 1 book and author pair on each line of the file (example: Douglas Adams,The Hitchhiker's Guide to the Galaxy). I am having trouble with how I should either tokenize or split the string so that I can insert the author and the title of the book into two separate arrays by using the comma as a delimiter. Any help is appreciated.
The size of the arrays are defined by the capacity parameter in the function. The arrays are allocated prior to calling the readBooks() function, so there is no need to dynamically allocate them.
Here is the code I have so far:
int readBooks (string filename, string titles[], string authors[], int books, int capacity){
ifstream file;
file.open (filename);
if (file.fail()){
return -1;
}
else{
int i = 0;
int j = 0;
while (i < capacity){
string line;
getline (file, line);
if (line.length() > 0){
}
}
}
}