std::ifstream.read() comes up with random text

Started by
16 comments, last by Zahlman 12 years, 5 months ago
So every frame i call a function that reads a little bit of a text file, like this:



void Loader::Continue()
{
for(int newB=0;newB<LOADBUFSIZE;newB++)
{
Loader::At++;
Loader::LoadFrom.seekg(Loader::At,std::ios::beg);

char nBuf[LOADBUFSIZE];
Loader::LoadFrom.read(nBuf,LOADBUFSIZE);
Loader::Buf<<nBuf;
APP.Log.Write(nBuf);
}
if(Loader::Done())
{
Loader::Close();
}
}



But comes up with garbled text when i read what it loaded in the log file. Am i doing anything wrong? What should i do?
Advertisement
What is this supposed to do? And what does "garbled text" mean?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

It reads part of a text file, puts it into a char array, then adds it to a stringstream. garbled text is un-formatted re-formatted data, like for example from what it shows: b5c0x28fb5c0x28fb5c0x28fb5c0x28fb5c0xÌHL
which should be something like |1|5|1|1|1|1|1| because that is the map format.
Looks like it's printing out memory addresses (the same memory address for that matter, at 0x28fb5c0; set a debug break point there, check the addresses of your variables, and see what it's printing out; you should see it's printing out the address of one of the variables, most likely). Be careful with reading data like that though. I've no idea what Loader::Buf is (it'd help if we knew what it was), but if it's expecting nBuf to be a null terminated string, you're in trouble because std::ifstream::read will not null terminate the string. It just simply reads raw data into the buffer. Same goes for APP.Log.Write(nBuf)

If you want nBuf to be null terminated, initialize the entire array to zero, and then only read in LOADBUFSIZE - 1 bytes. There's another way but I think this one is easiest.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
So how would after initializing the entire nBuf array to 0, and then reading to LOADBUFSIZE-1, what should i do to turn that raw data into a usable std::string?

So how would after initializing the entire nBuf array to 0, and then reading to LOADBUFSIZE-1, what should i do to turn that raw data into a usable std::string?


std::string myString = nBuf;

or

std::string myString(nBuf);


It looks like your file is stored as text, so the "raw data" is really just text. The only thing I'm suggesting is you null terminate [font="Courier New"]nBuf[/font], because [font="Courier New"]std::istream::read[/font] won't do it for you.

Alternatively, you could load the file straight into the [font="'Courier New"]std::string[/font] one line at a time by doing

std::string myString;
std::getline(Loader.LoadFrom, myString);


Assuming [font="Courier New"]Loader.LoadFrom[/font] is an [font="Courier New"]std::ifstream[/font] (and therefore an [font="Courier New"]std::istream[/font]).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
the file is 23.4 kb in size, and is all in one line, so i dont think loading the whole thing will work... and when i used std::string(nBuf) it still had the garbled text.

the file is 23.4 kb in size, and is all in one line, so i dont think loading the whole thing will work...


Why, because you don't have 23.4 kilobytes of RAM on your computer? I've got 8 gigs of RAM on my laptop, so I could load your file into memory over 330,000 times before requiring my computer to do any paging... A single string of size 23.4 kilobytes isn't that bad.

Either way, you can also process it in chunks as well. It's up to you. I guess if you're working in a seriously memory limited environment then you're forced to, but after loading the file I bet it takes more than 23.4 kilobytes of memory anyway.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
well, i have a single thread application, so im bottle necked. i need to load it chunk by chunk though... so it loads without pausing the application.

well, i have a single thread application, so im bottle necked. i need to load it chunk by chunk though... so it loads without pausing the application.


It takes longer to fetch the file chunk by chunk. But if you want to update things as it loads then cool, that's totally fine. I'd still be surprised if it took more than half a second to load a 23.4KB file into memory though. But it's fine, you can load it in chunks. I'm not necessarily saying you shouldn't.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement