Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

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

This topic is 2576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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]).

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!