Archived

This topic is now archived and is closed to further replies.

Samith

reading a string of variable length from a file?

Recommended Posts

Samith    2460
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?

Share this post


Link to post
Share on other sites
Zipster    2359
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.

Share this post


Link to post
Share on other sites
Samith    2460
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]

Share this post


Link to post
Share on other sites
Zipster    2359
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;


Share this post


Link to post
Share on other sites
Samith    2460
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)

Share this post


Link to post
Share on other sites
Zipster    2359
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).

Share this post


Link to post
Share on other sites
Samith    2460
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?

Share this post


Link to post
Share on other sites
Zipster    2359
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.

Share this post


Link to post
Share on other sites