do filenames passed to functions have to end in newline/be null terminated

This is a discussion on do filenames passed to functions have to end in newline/be null terminated within the C++ Programming forums, part of the General Programming Boards category; do filenames passed to functions have to end in newline/be null terminated specifically ifstream::open...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    do filenames passed to functions have to end in newline/be null terminated

    do filenames passed to functions have to end in newline/be null terminated

    specifically ifstream::open

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    String should be NULL terminates. The filename is no exception.

    Kuphryn

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One good practice when dealing with strings is to insert NULL right after dynamic allocation.

    Code:
    size_t stringSize = strlen(pMaterial->m_texture);
    
    char *temp = new char[stringSize + 1];	
    temp[stringSize] = '\0';
    strcpy(temp, pMaterial->m_texture);
    temp[0] = '/';
    Kuphryn

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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

    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;
    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)

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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)
    No, no, no!
    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

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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?
    thus I think milkshapes paths are in fact the correct ones
    doing a straight copy does not work
    wtf am I just dumb or something?

    Code:
    	mpMaterials[nummaterials].mTexFilename = new char[strlen(pMaterial->m_texture) + 1]; 
    	strcpy(mpMaterials[nummaterials].mTexFilename, pMaterial->m_texture);
    filename produced:
    .\cave_gold.bmp
    Last edited by Silvercord; 02-09-2003 at 12:40 PM.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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 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.

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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.

  11. #11
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    here is what i mean
    Attached Images Attached Images  

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Take it out of your debug folder and put it into your main project folder if you're running it from the compiler.

  13. #13
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    already done...

  14. #14
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 02:48 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21