Thread: Appending to the end of a char*

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19

    Appending to the end of a char*

    Okay here is the psuedocode of what I want to do

    void openFile(int inputNumber)
    {

    char* = "//folder//" + inputNumber + ".txt"

    }

    unfortunately that is not how you append to the end of a char*.

    Any suggestions of doing this?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    strcat()

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am guessing that you are trying to construct a filename. One way:
    Code:
    std::stringstream ss;
    ss << "//folder//" << inputNumber << ".txt";
    std::string filename = ss.str();
    Now you can use filename.c_str() with say, the constructor of std::ifstream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    Okay I am about to go try that, but what exactly is stringstream, it looks like cout all over, but copied into a string.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    but what exactly is stringstream, it looks like cout all over, but copied into a string.
    That's the general idea. You should #include <sstream> and <string>, of course.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    splendid also how did you get a signature?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by thegr8n8 View Post
    Okay here is the psuedocode of what I want to do
    Code:
    void openFile(int inputNumber)
    {
    
        char* = "//folder//" + inputNumber + ".txt"
    
    }
    unfortunately that is not how you append to the end of a char*.

    Any suggestions of doing this?

    For that you'd need a sufficiently large buffer and something like "sprintf" to copy the values to text. Of course, the standard C++ approach is to use an std::stringstream (which works just like the 'cout' object (ie: file streams)). It's "str" member function returns an std::string, whereby you can use either the "data" or "c_str" function to retrieve the const char* that you need to open the file (kind of convoluted, yes!).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    how did you get a signature?
    You may need to hang around for awhile and post before the option becomes available to you

    Quote Originally Posted by Sebastiani
    whereby you can use either the "data" or "c_str" function to retrieve the const char* that you need to open the file
    Use c_str(), not data(), as the latter does not guarantee a null terminator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    bad conversion from const char* to char*.....

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    bad conversion from const char* to char*.....
    That error message is meaningless in the absence of corresponding code. I can assure you that my example is correct, and that your compiler and I agree that your code is wrong
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    lol well sorry this was an error message for a diffrent thing I was doing:
    string playerName = "Nathan";
    char* = playerName.c_str();


    that is where the error message appears at

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do that when you already have a string object? Furthermore, you are missing a variable name and c_str() returns a const char* which cannot be assigned to a char*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    Use c_str(), not data(), as the latter does not guarantee a null terminator.
    Bah! Right.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    ah I see now, so how would one go from string to regular char* then?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    so how would one go from string to regular char* then?
    There are a few ways... but why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. appending some letters to the end of a CString variable.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 03:00 PM