HI,
I need to tokenize the data from a file named books.dat to load into the system...anyone got any clues?! some help would be greatly appreciated
Book data file format:
<id>:<title>:<author>:<year>:<price>:<count>
Example:
1:A Book on C:A. Kelly:1997:89.95:7
2:Java Software Solutions:J. Lewis:2000:79.95:2
etc...
n so far my code is looking like this...
Code:
#include "bookstore2.h"
#define NUM_ARGUMENTS 3
#define MAXSTRING 100
#define ARRAYSTR 100
int main(int argc, char **argv)
{
int tempID, tempYear, tempPrice, tempCount;
char tempString[MAXSTRING], DELIMITER[] = ":";
char* ptr;
char* tempArray[ARRAYSTR];
char* booksFile;
char* salesFile;
BookListType booklist;
FILE* fp;
/*when arguments are wrong*/
if(argc != NUM_ARGUMENTS)
{
fprintf(stderr, "Argument error\n");
return EXIT_FAILURE;
}
else
{
/*check what arguments were passed*/
if((argv[1] == "books.dat") && (argv[2] == "sales.dat"))
{
loadData(&booklist, booksFile, salesFile);
fp = fopen("books.dat", "r");
/*read data from books.dat*/
ptr = strtok(tempString, DELIMITER);
tempID = strtol(ptr, &tempArray, 10);
newBook->id = tempID;
ptr = strtok(NULL, DELIMITER);
strcpy(newBook->title, ptr);
ptr = strtok(NULL, DELIMITER);
strcpy(newBook->author, ptr);
ptr = strtok(tempString, DELIMITER);
tempYear = strtol(ptr, &tempArray, 10);
newBook->year = tempYear;
ptr = strtok(tempString, DELIMITER);
tempPrice = strtol(ptr, &tempArray, 10);
newBook->price = tempPrice;
ptr = strtok(tempString, DELIMITER);
tempCount = strtol(ptr, &tempArray, 10);
newBook->count = tempCount;
if(fp == NULL)
{
printf("Can't open file\n");
exit(1);
}
fclose(fp);
}
}
return EXIT_SUCCESS;
}
.....with these errors when compiled:
bookstore2.c: In function `main':
bookstore2.c:41: warning: passing arg 2 of `strtol' from incompatible pointer type
bookstore2.c:42: error: `newBook' undeclared (first use in this function)
bookstore2.c:42: error: (Each undeclared identifier is reported only once
bookstore2.c:42: error: for each function it appears in.)
bookstore2.c:51: warning: passing arg 2 of `strtol' from incompatible pointer type
bookstore2.c:55: warning: passing arg 2 of `strtol' from incompatible pointer type
bookstore2.c:59: warning: passing arg 2 of `strtol' from incompatible pointer type
anyone know how to fix??