Hello, I have done quite a bit of searching for a solution, and have implemented something similar, but am still getting a bug. The function that I think is giving me problems is:
Code:
int copyFromChar(const char * buff, int begin, int end) {
char * temp = NULL;
temp = malloc(end-begin);
int j = 0;
for (int i=begin; i<end; i++, j++) {
temp[j] = buff[i];
}
// memmove(temp, buff + begin, (end-begin));
if ((strcmp(temp, "Other Picnic Table") == 0) || strcmp(temp, "Aggregate") == 0) {
free(temp);
return 0;
}
if ((strcmp(temp, "Round Picnic Table") == 0) || strcmp(temp, "Composite") == 0) {
free(temp);
return 1;
}
if ((strcmp(temp, "Square Picnic Table") == 0) || strcmp(temp, "Concrete") == 0) {
free(temp);
temp = NULL;
return 2;
}
...
I have a buff
Code:
char *buff = NULL;
buff = malloc(250);
declared in the parent function.
Then I malloc a bunch of memory for my other structs, and I iterate through a .csv file with
Code:
while ((c=fgetc(IN)) != EOF) {
if (c == ',' && row > 0) {
DB->picnicTableTable->entry[dbIndex] = malloc(sizeof(Entries));
switch (column) {
case 0: // id
id = numberfy(buff, 0, i);
begin = i+1; // this sets up the counter so "begin" starts right after the
column++; // comma and I have the correct range of buff for inserting
break;
case 1: // Table Type
tableType = copyFromChar(buff, begin, i);
begin = i+1;
column++;
break;
...
Sometimes my return value from copyFromChar() is correct (should be 2), and sometimes it returns my error flag (int 6). Stepping through line by line, I can see that the string copied to temp sometimes contains more data from buff than it's supposed to, but other times it is perfect.
The for loop and the memmove are both prone to the same error, which makes me think that my problem is the area of buff that is being grabbed, but I can't for the life of me see what is wrong.
Could a kind soul point me in the right direction?