Since you seem to be parsing XML, how about using a pre-existing XML parser library?
The Expat XML Parser
It's a bit of extra work up front, but at least you won't have to worry too much about feature creep or minor changes in the text file which might otherwise break a simpler parser.
3 quick hacks for reading files
Code:
// Good - file is stored as a linked list of lines
struct linelist {
struct linelist *next;
char buff[BUFSIZ];
};
struct linelist *head = NULL, *tail = NULL;
char buff[BUFSIZ];
while ( fgets ( buff, sizeof buff, fp ) != NULL ) {
struct linelist *temp = malloc( sizeof *temp );
if ( temp ) {
strcpy( temp->buff, buff );
temp->next = NULL;
if ( !head ) {
head = tail = temp;
} else {
buff->next = tail;
tail = temp;
}
}
}
// Bad - file is all in a single buffer, but expensive to create for long files
char *file = malloc(1);
*file = '\0';
char buff[BUFSIZ];
while ( fgets ( buff, sizeof buff, fp ) != NULL ) {
size_t newlen = strlen(file) + strlen(buff) + 1;
void *temp = realloc( file, newlen );
if ( temp ) {
file = temp;
strcat( file, buff );
}
}
// Ugly - cheap hack to store the whole file at once
// Makes some assumptions about how text files are read (see "rt" mode)
// Probably best not to use this one...
fseek( fp, 0, SEEK_END );
long len = ftell( fp );
rewind( fp );
char *file = malloc( len + 1 );
fread( file, 1, len, fp );
file[len] = '\0';