Thread: Add strings together

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Add strings together

    I have a string that I created and passed as a parameter to my output function. I need to add this string to an existing string as an out.open() parameter.

    This is what I tried to do:

    Code:
    out.open("page"+pageNo+".html");
    It's obviously not working but if anyone could help me that would be great. Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    open requires a C string, not a std::string. So create a temporary string object, set it to "page"+pageNo+".html", and then in the open call you would use tempstring.c_str().

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I am guessing pageNo is an int or something.

    Google std::stringstream.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by cyberfish View Post
    I am guessing pageNo is an int or something.

    Google std::stringstream.
    yeah I used sstream to convert the int to a string.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Just allocate some memory and use sprintf.
    sprintf(szBuffer, "page %d .html", pageNo);
    out.open(szBuffer);

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    No need to go C way here.

    Exploit the overloaded operators of std::string.

    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    Last edited by cyberfish; 12-04-2008 at 12:09 AM.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Doesn't he need
    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    ?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    hmm. Right .

    Thanks for the correction. Edited my post.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Here is the more Efficient, Lightweight way of doing what you want to do:
    Note: Only works with 1 digit, you'll need a function (like iota) if you want more digits etc.

    Code:
    int pageNo = 1;
    char page[] = "page       ";
    page[4] = '0'+pageNo;
    page[5] = '.';
    page[6] = 'h';
    page[7] = 't';
    page[8] = 'm';
    page[9] = 'l';
    
    // now page = page1.html
    out.open(page);
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have one more space at char page[] = "page "!

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by execute View Post
    Here is the more Efficient, Lightweight way of doing what you want to do:
    Note: Only works with 1 digit, you'll need a function (like iota) if you want more digits etc.

    Code:
    int pageNo = 1;
    char page[] = "page       ";
    page[4] = '0'+pageNo;
    page[5] = '.';
    page[6] = 'h';
    page[7] = 't';
    page[8] = 'm';
    page[9] = 'l';
    
    // now page = page1.html
    out.open(page);
    But also very error prone and quite tedious to type - yes, it's only the once, but if you have to do it slightly differentely (e.g. the "page" changes to some other word in a foreign language or some such), you have to count the characters, and get everything right. How muhc more work is it to use a more automatic method - considering that this probably ends up with either reading or writing some sort of html content from/to a file...

    --
    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.

  12. #12
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by matsp View Post
    But also very error prone and quite tedious to type - yes, it's only the once, but if you have to do it slightly differentely (e.g. the "page" changes to some other word in a foreign language or some such), you have to count the characters, and get everything right. How muhc more work is it to use a more automatic method - considering that this probably ends up with either reading or writing some sort of html content from/to a file...

    --
    Mats
    I think that comment was firmly tongue in cheek

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is the wrong thing:
    Code:
    out.open("page"+pageNo+".html");
    this is the correct thing:
    Code:
    out.open((std::string("page")+pageNo+".html").c_str());
    They are not that different. So there is nothing more simple than the above as the correct answer.

    About efficiency I don't think there is a huge difference.
    I would expect them to have the same speed more or less. Of course you give the total size so it allocates only once. You could call string::reserve for that to be fair.
    But in any case you allocate space, initialize it, and write again on that memory.
    The above "correct" method allocates space, initializes it, allocates space, initializes, allocates space initializes it.
    Since the string is not really big there won't be probably any need to copy data for the std::string. So it is 3 allocations vs 1 allocation and rewriting a memory space (page[4]).
    Now, do this with reserve (which is fair). Now you allocate space, initialize on part, initialize other part, initialize other part.
    So it is 3 allocations+initialization and 1 rewrite vs 3 allocations+initialization. So the std::string method might actually be faster.

    Of course the std::string will have some function calls overhead. But those are not that big of a time waste. And they are totally worth it.

    But the point is that you might NOT know the size. Especially considering that pageNo WILL change. It might go over 9. So you would need one more space.
    Last edited by C_ntua; 12-04-2008 at 10:51 AM.

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Assuming the OP isn't optimizing for some tiny embedded system, and is already using sstream rather than implementing his own itoa() to convert the integer pageNo to a string, why not simply stick with stringstream:

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        std::string s;
        ss >> s;
        out.open(s.c_str());
    ...

    or, if you object to the overhead of std::string (and he's not already using it someplace anyway):

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        char cs[5];
        const char* cp = cs;
        ss >> cs;
        out.open(cp);
    ...
    Now the only added overhead is a 5-byte char array (assuming his page numbers are no more than 4 digits) and a const char*.
    Last edited by R.Stiltskin; 12-04-2008 at 12:19 PM.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by R.Stiltskin View Post
    Assuming the OP isn't optimizing for some tiny embedded system, and is already using sstream rather than implementing his own itoa() to convert the integer pageNo to a string, why not simply stick with stringstream:

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        std::string s;
        ss >> s;
        out.open(s.c_str());
    ...

    or, if you object to the overhead of std::string (and he's not already using it someplace anyway):

    Code:
    ...
        std::stringstream ss;               // he already has the equivalent of this in his code
        int pageNo = 31;                    // he already has the equivalent of this in his code
        ss << "page" << pageNo << ".html";  // he already has 'ss << pageNo;' in his code
        char cs[5];
        const char* cp = cs;
        ss >> cs;
        out.open(cp);
    ...
    Now the only added overhead is a 5-byte char array and a const char*.
    Well, it might work for this situation. But if you want it to be both valid in case the filename would be "page #.html" (with space) as well, and faster, you could use:
    Code:
    ss << "page" << pageNo << ".html";
    out.open(ss.str().c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM