reading a string of variable length from a file?

Started by
6 comments, last by Samith 20 years, 9 months ago
It''s been quite a while since I have done any programming (school got in the way, now I''m just starting to get back into the groove of coding) So I have this problem. Let''s say I want to have a 2d tilemap, and inside that map''s header is the filename of the tileset. I want this file name to be as short or as long as you feel like having it, but I don''t know how to make the program read up until it gets to a certain character (say I want it to read up until it finds a $, then it would know the filename is over.) So, yeah. How would you do this?
Advertisement
You can either store the length of the filename before the filename string itself, or like you said, create a delimiting value and read character by character until you reach your delimiter.
yeah but how do I do the delimiter thing? this is what I came up with:
do {int x = 0;*(texture+x) = getc(filepointer);x++;} while(*texture!='$');


Would that work? Sorry if this is just retarded but I'm a bit sketchy here after not coding for so long

[edited by - Samith on July 7, 2003 11:54:52 PM]

[edited by - Samith on July 7, 2003 12:01:39 AM]
Initialize the variable outside the loop, otherwise it will just reset to 0 each time. And subscript notation would be cleaner. You should also make sure the buffer itself is big enough. You might want to add an overflow check also, so you don''t break the bounds. There''s also a few minor corrections:

int x = 0;do {texture[x] = getc(filepointer);x++;} while(texture[x-1]!=''$'' && x < MAX_SIZE);texture[x-1] = 0;


quote:Original post by Zipster
Initialize the variable outside the loop, otherwise it will just reset to 0 each time. And subscript notation would be cleaner. You should also make sure the buffer itself is big enough. You might want to add an overflow check also, so you don''t break the bounds. There''s also a few minor corrections:

int x = 0;do {texture[x] = getc(filepointer);x++;} while(texture[x-1]!=''$'' && x < MAX_SIZE);texture[x-1] = 0;




Thanks a lot, I would have missed those stupid mistakes

Anyway, later on if I want to use ''texture'' as the filename to load a tileset, will it load filename.ext or filename.ext0 (because you set texture[x-1] to 0)
I assigned it as the literal value 0, which is the NULL terminator for your string (since ''$'' is only the delimiter you use, but 0 is the "real" end-of-string delimiter).
Ahh I see. I know I have a lot of questions...but here is one more: Is there any way I could do this with fread? Or should I just stick with the getc method?
You have to tell fread how many bytes to read. This would only work if you used the method I first mentioned, which was storing the number of characters in the filename beforehand. I like that method better and it is a cleaner method code-wise, as you don''t need this one-character-at-a-time-until-we-reach-our-special-delimiter bullcrap.

This topic is closed to new replies.

Advertisement