need help with string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: need help with string

  1. #1
    Join Date
    Dec 2009
    Posts
    4

    Exclamation need help with string

    I have three variables:

    const char* dir //directory of file

    int i //the file's name without extension

    const char* ext //the extension of the file

    how do I add these together to get a const char* string that should look like this:

    "img/1.bmp"

    Thanks,



    ~justin123

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    Re: need help with string

    sprintf would be one way.

  3. #3
    Join Date
    Dec 2009
    Posts
    4

    Re: need help with string

    would this work:

    sprintf(name,"%d",dir+i+ext);

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: need help with string

    are you using C or C++??

    In C++ you would use std::string and stringstreams.

    In C you would use char* and sprintf. sprintf is pretty similar to printf except it prints to an area in memory and not the screen. If that sprintf were a printf, do you think it would print on the screen what you want?
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,121

    Re: need help with string

    Quote Originally Posted by justin1232 View Post
    would this work:

    sprintf(name,"%d",dir+i+ext);
    No.

  6. #6
    Join Date
    Dec 2009
    Posts
    4

    Re: need help with string

    im using c++ and i have no clue on how to do this.

  7. #7
    Join Date
    Jun 2008
    Posts
    592

    Re: need help with string

    the c++ way

    Code:
    #include <string>
    #include <sstream>
     
    template< class Type >
    std::string ToString( Type Input )
    {
        std::stringstream Out;
        Out << Input;
        return Out.str();
    }
     
    using namespace std;
    string File = ToString( Dir ) + ToString( I ) + ToString( Ext );
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  8. #8
    Join Date
    Dec 2009
    Posts
    4

    Re: need help with string

    that part works, but how do i convert "file" to a const char* string so the function thats using it can understand it?

  9. #9
    Join Date
    Jun 2008
    Posts
    592

    Re: need help with string

    File.c_str();

    http://www.cplusplus.com/reference/string/string/

    is this function yours? if so, you should change it to

    void Input( const string& File );
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center