Page 1 of 1

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

Posted: Mon Mar 12, 2018 9:21 pm
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

Re: Bug in IFileSystem::createAndOpenFile || IReadFile

Posted: Mon Mar 12, 2018 11:52 pm
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;

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

Posted: Tue Mar 13, 2018 10:32 pm
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?

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

Posted: Wed Mar 14, 2018 2:57 am
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.