do filenames passed to functions have to end in newline/be null terminated
specifically ifstream::open
do filenames passed to functions have to end in newline/be null terminated
specifically ifstream::open
String should be NULL terminates. The filename is no exception.
Kuphryn
I'm having trouble converting a character array from something saved in a milkshape3d file to something that can be used. MS3D saves their filenames as .\filename. I need to:
take out the period
convert any back slashes to forward slashes (only forward slashes or double back slashes can be used when addressing filename directories)
I've got some code and it produces this filename, but it always says it cannot find the file. It is a relational filename not the absolute filename (which is how it's supposed to be, I think)
What my code produces, this doesn't have a newline character in it I don't think
/cave_gold.bmp
When I try to manually append a newline character I get this filename:
/cave_gold.bmp
Íýýýý
Notice it has a newline character in it(that's why it went to the next line, I guess) but it has those other characters. Here is the code that I'm using to extract the filename, what should I change? Note pMaterial has what's in the file itself
Code:ofstream fout("test.txt", ios::out | ios::binary); //non standard, therefore no ios::nocreate //mpMaterials is my material structure, i'm trying to convert stuff from pmaterial to my mpmaterials mpMaterials[nummaterials].mTexFilename = new char[strlen(pMaterial->m_texture)]; char *temp = new char[strlen(pMaterial->m_texture)]; strcpy(temp, pMaterial->m_texture + 1); //copy only the slash and filename, not the period temp[0] = '/'; //convert to usable forward slash temp[strlen(temp)] = '\n'; //append newline character strcpy(mpMaterials[nummaterials].mTexFilename, temp); fout << mpMaterials[nummaterials].mTexFilename;
Last edited by Silvercord; 02-09-2003 at 12:04 PM.
One good practice when dealing with strings is to insert NULL right after dynamic allocation.
KuphrynCode:size_t stringSize = strlen(pMaterial->m_texture); char *temp = new char[stringSize + 1]; temp[stringSize] = '\0'; strcpy(temp, pMaterial->m_texture); temp[0] = '/';
Hmm, I did that and it still says it cannot load the textures and the model shows up white (untextured). I know I've got the actual placement of the files in the correct place (they're both in the same folder which should be working). This is my updated code
I'm going to open the bitmap and read in a bitmapinfoheader structure and test it to see if it works using my filename. IF it does work then I'm going to have to check for an 'external' problem (i.e me being a dumbass elsewhere in the program)Code:ofstream fout("test.txt", ios::out | ios::binary); size_t length = strlen(pMaterial->m_texture); //mpMaterials is my material structure, i'm trying to convert stuff from pmaterial to my mpmaterials mpMaterials[nummaterials].mTexFilename = new char[strlen(pMaterial->m_texture)]; char *temp = new char[strlen(pMaterial->m_texture) + 1]; temp[length] = '\0'; strcpy(temp, pMaterial->m_texture + 1); //copy only the slash and filename, not the period temp[0] = '/'; //convert to usable forward slash strcpy(mpMaterials[nummaterials].mTexFilename, temp); fout << temp;
No, no, no!Originally posted by Silvercord
MS3D saves their filenames as .\filename. I need to:
take out the period
convert any back slashes to forward slashes (only forward slashes or double back slashes can be used when addressing filename directories)
Back slashes can -- while double back slashes can not -- be used in path names.
Double back slashes are only a way to represent single back slashes in source code.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
The double backslashes that you see in dos style paths are because the \ is C++'s escape character, to produce it literally you use it twice. The actual system call expects single slashes(though they also work with the "correct" normal slash /) again calling open("C:\\new\\results.txt") sends the string C:\new\results.txt, if you already have the string C:\ whatever, there is no need to convert the backslashes to double backslashes. The double backslash is an instruction to your compiler not the system call. Most system calls do not expect newlines.
A '.' in path name stands for the current directory, thus I think milkshapes paths are in fact the correct ones. I also strongly suggest using a std::string to do any cleverness.
edit: beaten, dang.
I wasn't converting to double back slashes, I was converting to forward slashes. are you saying I need to leave the period in there as well and just do a straight copy of the filename?
doing a straight copy does not workthus I think milkshapes paths are in fact the correct ones
wtf am I just dumb or something?
filename produced:Code:mpMaterials[nummaterials].mTexFilename = new char[strlen(pMaterial->m_texture) + 1]; strcpy(mpMaterials[nummaterials].mTexFilename, pMaterial->m_texture);
.\cave_gold.bmp
Last edited by Silvercord; 02-09-2003 at 12:40 PM.
I don't think you are dumb, but where is cave_gold.bmp? If it's in the same directory as your program then that should work. How do you open the milkshape file? If cave_gold has the same prefix you should be fine.Originally posted by Silvercord
I wasn't converting to double back slashes, I was converting to forward slashes. are you saying I need to leave the period in there as well and just do a straight copy of the filename?
doing a straight copy does not work
wtf am I just dumb or something?
<snip>
filename produced:
.\cave_gold.bmp
I'm building the debug version, and I have a copy of the .ms3d file in the debug folder along with the bitmap. When I build and run the project I've got another copy of the .ms3d ifle and the bitmap in the project folder (with all the other source files). Point being the bitmap is always in the same directory as the .ms3d model file. HmmMMmmm. Strange.
here is what i mean
Take it out of your debug folder and put it into your main project folder if you're running it from the compiler.
already done...
Just to let you guys know my problem MUST be external, I've got a few working versions of this project, I'm drawing several textured cubes using the filename supplied with the milkshape model, but whenever I create a vector of more than one element of the model entities texturing does not work. To top it all off I cannot seem to ever get texturing working again in the same project after I've done this. To show you how my system is set up:
vector<GameEntity*> Clients;
//Player derives from gameentity
//Player has weapons and hulls which derive from model
...
for(int i = 0; i < 25; i++) {
Clients.push_back(new Player);
}
...
for(int i = 0; i < Clients.size; i++) {
glTranslatef(i * 2, i * 2, i * 2);
Clients.at(i)->CallDraw(); //draws the cubes without textures
}
...
And it does not seem to be a problem of derivation for I can have a single gameentity pointer which is a new player being drawn correctly.
OMG