Thread: File Paths and functions

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    17

    File Paths and functions

    Well I am trying to mimic my old c++ code of the map readers. But I am having lot of trouble here is what is suppose to happen:

    3 parts to the string FilePath
    1.The file path to where to find the map.
    2. The file name
    3. The ending (.txt).

    all of these is then merged with strcat. As maps change every time a player moves the file name changes. E.g.

    c:\ (file path).
    1 (the map name)
    .txt (map ending).

    These strings are then merged into 1 file path "c:\1.txt"

    My function for this works fine which can be found here:
    Code:
    char changeMap(char mapName[5]){
    
        strcat(mapPath,mapName);
        strcat(mapPath,mapEnd);
    
        return(mapPath[50]);
        
         }
    All of this works fine if I put in the string my self .e.g chnagemap(3); will make it read from the file c:\3.txt. How ever when I try and use a variable for the value it does not work e.g.

    changeMap(player.map);

    any help on the matter?

    thanks
    zidsal

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    return(mapPath[50]);
    So you want to return just one character, the 51st of the string? Because that's what you are in fact doing.

    Generally, trying to "return an array", which is what I think were actually attempting to do, is a bad idea - it is NEARLY NEVER the right thing. You can return the address of a string, but that is also a bit tricky. In this case, I'd say you probably want to add another parameter to the function that holds a string with space for the full filename to be filled into. Don't forget to pass the max size of the full name, so that you can ensure that you don't go over the allowed space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    Quote Originally Posted by matsp View Post
    Code:
    return(mapPath[50]);
    So you want to return just one character, the 51st of the string? Because that's what you are in fact doing.

    Generally, trying to "return an array", which is what I think were actually attempting to do, is a bad idea - it is NEARLY NEVER the right thing. You can return the address of a string, but that is also a bit tricky. In this case, I'd say you probably want to add another parameter to the function that holds a string with space for the full filename to be filled into. Don't forget to pass the max size of the full name, so that you can ensure that you don't go over the allowed space.

    --
    Mats
    thanks and about the mapPath[50] that was just expirmenting trying to work it out my self. Thanks for the advaice I have a stab at it.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zidsal View Post
    thanks and about the mapPath[50] that was just expirmenting trying to work it out my self. Thanks for the advaice I have a stab at it.
    Yes, I sort of guessed that you didn't get it from the book you are learning from ... I was just trying to point out what it ACTUALLY does, and let you figure out why it's NOT what you wanted. It's a common mistake.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unfamiliar syntax
    By bean66 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2009, 12:36 PM
  2. Help please: program using functions
    By ZeroBurn7 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2005, 05:38 PM
  3. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM
  4. Linker quetion
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 10-25-2004, 09:02 PM