[no]Bug in IFileSystem::createAndOpenFile || IReadFile

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Dreadstew
Posts: 23
Joined: Wed Apr 01, 2015 5:18 pm

[no]Bug in IFileSystem::createAndOpenFile || IReadFile

Post by Dreadstew »

Hi, I load a file two different ways. Irrlicht has exponents tacked onto the end. Must have happened in openFile or ReadFile. Not sure. But it makes the irrlicht file loader unusable for me.

Code: Select all

io::IReadFile* ini = filesystem->createAndOpenFile(io::path("C:/Users/cool_/Documents/VisualStudio2017Projects/GoldenAgeClient/x64/Assets/ini.txt"));
char* buffer = (char*) malloc(ini->getSize());
ini->read(buffer, ini->getSize());
stringstream ss;
ss << buffer;
cout << ss.str() << endl;
 
ifstream t("C:/Users/cool_/Documents/VisualStudio2017Projects/GoldenAgeClient/x64/Assets/ini.txt");
stringstream buf;
buf << t.rdbuf();
cout << buf.str() << endl;
The log:
#This should be set to your "Assets" folder
WorkingDirectory = C:/Users/cool_/Documents/VisualStudio2017Projects/GoldenAgeClient/x64/Assets²²²²
#This should be set to your "Assets" folder
WorkingDirectory = C:/Users/cool_/Documents/VisualStudio2017Projects/GoldenAgeClient/x64/Assets
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Bug in IFileSystem::createAndOpenFile || IReadFile

Post by CuteAlien »

It's not Irrlicht adding stuff, you simply have that stuff randomly in your memory. The problem you are having is that you print a string which is not null-terminated. That's unless your file accidentally ends with 0 - which is usually not the case, especially not for text-files. So it prints the string until you have some random 0 somewhere in memory. This is not an Irrlicht thing but something you have in general when reading files into a string-buffer (so the exact same with fopen for example). What you need to do is to allocate a buffer with 1 byte more than file-size. And then terminate the buffer yourself with buffer[ini->getSize()] = 0;
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Dreadstew
Posts: 23
Joined: Wed Apr 01, 2015 5:18 pm

Re: [no]Bug in IFileSystem::createAndOpenFile || IReadFile

Post by Dreadstew »

Oh I should have known! So I don't get this problem with ifstream because I don't use ios::binary (or something like that). Maybe provide a text file version of createAndOpenFile?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [no]Bug in IFileSystem::createAndOpenFile || IReadFile

Post by CuteAlien »

It's not a stream class but the equivalent of fopen (which is even used in this case). So it's a little bit hard to compare exactly. fopen also has a text-mode but that wouldn't do the same. Actually it's not even defined _what_ that text-mode does, that's up to the implementation and different system would do different things. It might do some endline conversions which will change the amount of bytes even more (fopen has a rather unfortunate default and generally you want to use fopen with "b" flag in nearly every case and forgetting it will drive you crazy in debugging until you see it).

ifstream is a little bit nicer - and I actually recommend using the c++ stream classes when you can. You can mix STL and Irrlicht code - I do that all the time.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply