Boy you sure like overcomplicating things. For starters, why aren't you just using arrays? I mean, it's not like you're resizing anything anyway. Next, you can do all of this with one single buffer. This is what you should be doing:
Code:
while not at the end of the file
read a line
if line == only whitespace
continue
if line == CD
begin a new record
if line == artist
stick artist info into record
if line == title
...
if line == year
...
It can even be simpler than that if you're sure they'll always have the same format. If you want it to be safe, you do the above with a few variables to track what you've already got in the record.
You then decide what you do with info that isn't right:
-CD-
Artist: Jay Z
Title: Reasonable Doubt
Title: Reasonable Foo
Year: 1993
Now here we have two title lines. One of these shouldn't be there. You have to account for something like this.
At any rate, you're overcomplicating this. You don't need strtok at all. You should however use something like this strstr, because it'll be much simpler that way. I'll do this in C, because it seems you like C strings anyway...
Code:
while( fgets( buf, BUFSIZ, file ) != NULL )
{
if( strstr( buf, "-CD-" ) )
{
//we've found the CD line, so just skip it or make a new record
//or whatever it is you've decided to do. I'll skip it.
continue;
}
if( strstr( buf, "Artist:" ) )
{
//Ok, we've got a line starting (or at least containing) "Artist:"
//Do something with it...
}
...
}
And so on. You can even simplify it further if you know your data won't be screwed up.
Code:
while( fgets( buf, BUFSIZ, file ) != NULL )
{
if( !strstr( buf, "-CD-" ) )
continue; // It's NOT found, so keep looking...
fgets( artistbuffer, BUFSIZ, file );
fgets( titlebuffer, BUFSIZ, file );
fgets( yearbuffer, BUFSIZ, file );
//now peel off the first part of each and copy them where you want
}
But again, this works if you're using records which won't ever be "broken" or incorrect. You could of course skip those three fgets lines and use something like fscanf to read past the "tag"/prefix, and then use fgets to pick up from there.
Or you could use some of the *stream functions instead of C sytle functions to read your file.
At any rate, I'd suggest rethinking how you're doing it, because as I've stated, you're overcomplicating the issue.
Quzah.