Appending to the end of a char*

This is a discussion on Appending to the end of a char* within the C++ Programming forums, part of the General Programming Boards category; Okay here is the psuedocode of what I want to do void openFile(int inputNumber) { char* = "//folder//" + inputNumber ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
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, 02:00 PM

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