I'm having a hard time understand what it is you're supposed to be doing exactly.
What is the input specification? Do you need to store your output in a two dimensional array of strings? If so, your "ptr" parameter is declared incorrectly.
Since a string in C is an array of characters, you then need:
A pointer -> to an array -> of strings
char * -> [arraySize] -> [stringSize] myArray;
Which is basically a char***.
Anyway, from the little bit of info you gave about the problem, I managed to come up with a function that reads in a single record which seems similar to what you were trying to accomplish. But keep in mind, this isn't tested and it makes some assumptions that are almost certainly invalid.
Obviously, I wrote ignore_whitespace and ignore_non_nl_whitespace myself, and they have easy enough implementations.
PHP Code:
[code]
/*
Reads a single record from the input file into the array of strings
"ptr". It is assumed that memory has already been allocated for
the array, but NOT for each individual string in the array.
A single record is assumed to be in the format:
{ field1,field2,field3,field4,field5 }
and is expected to be all on one line. Reading stops
when the '}' character is read, or if an error occurs.
Each field is stripped of leading and trailing whitespace, and empty
fields are read as empty strings, so:
{} gives the array {""}
{,} gives the array {"", ""}
{blah, ,blah,} gives the array {"blah", "", "blah", ""}
Returns the number of fields contained in the record, or a negative
number if an error occurred.
*/
int read_single_record(FILE *fp, char **output)
{
char buffer[128]; // Temporary storage for a single field
char *buf = buffer; // A pointer into buffer (to make manipulation easier)
char **out = output;
char ch;
ignore_whitespace(fp); // ignore whitespace before the record
ch = fgetc(fp);
if (ch != '{') {
// The input is not formatted properly (no leading '{')
if (ch != EOF) ungetc(ch, fp);
return -1;
}
ignore_non_nl_whitespace(fp);
// read in comma separated fields until a '}' is reached
while(1) {
ch = fgetc(fp);
if (ch == ',' || ch == '}') {
// Trim trailing whitespace from the field
while (buf > buffer && isspace(buf[-1])) buf--;
*buf++ = '\0';
// Allocate output space for the field and copy the buffer there
*out = malloc(buf - buffer);
if (*out == NULL) return -2;
strcpy(*out++, buffer);
buf = buffer;
// If there is another field, ignore the whitespace in front of it
if (ch == ',') ignore_non_nl_whitespace(fp);
// If this was the end of the record, break out of the loop
if (ch == '}') break;
}
else if (ch == '\n') {
ungetc(ch, fp);
return -3;
}
else if (ch == EOF)
return -4;
else
*buf++ = ch;
}
return out - output;
}
[/code]
Btw, I wanted to use the nifty PHP hilighting, but it wouldn't let me post unless I put code tags around it as well.