I used to use ifstream everywhere and used getline(ifs,line) to get a line of text. I'm trying to create a function that does the same thing. Instead of using streams I'm now creating my own File class which utilizes PhysFS.
Here is my class:
It seems to almost work, but not quite, a few of my files and a few of my resources are not having lines read correctly from them. Is there something I'm not considering?
For example, I have code that reads backgrounds:
Sample Background file:
@default background Name: Blue Marble Floor Background: default.bg.png OffsetX: 0.225 OffsetY: 0.225 Scale: 1.0 Thumbnail:
Although it seems like it loads all 6 parameters for the background, but the code never reaches the new Sprite call. Certain files load fully and some fail to read certain lines. I'm puzzled.
Thanks
Sledgehammers to swat mosquitoes...
I was aware of that but was hoping to have an arbitrarily long line, not something like char tmpBuff[4096].
So if strlen() says your buffer is full, realloc() it to be larger and try again. Oh, wait, C++ can't do that, sorry.
Changed return !eof(); to return true and that fixed it. And I didn't even need realloc . Thanks
So if strlen() says your buffer is full, realloc() it to be larger and try again. Oh, wait, C++ can't do that, sorry.
C++ can call both strlen and realloc in exactly the same way as C, but that method wouldn't work anyway.
Also, constantly appending individual characters to a C++ string will result in a number of realloc calls internally, so deciding on a maximum buffer size, running al_gets and then creating a string from that. Its less flexible, but its faster.
The way I get around that is by calling str.reserve(512) or something. This way it only reallocs if I have something really big. For me this way offers more flexibility than a fixed size.
True. It still has the overhead of constant function calls, but that would provide a nice balance between flexibility and speed.