Add data onto string.

This is a discussion on Add data onto string. within the C Programming forums, part of the General Programming Boards category; I have a quick question, in php and I'm fairly sure C++, I can use the dot to append pieces ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Add data onto string.

    I have a quick question, in php and I'm fairly sure C++, I can use the dot to append pieces of a string together, is this possible in C.

    Code:
    strcat(output, temp);
    The aboves adds the contents of temp onto the string output but is their no method of doing this.

    Code:
    strcat(output, temp . "blah blah");
    strcat(output, temp + "blah blah");
    At the moment I'm having to call strcat twice.

    Code:
    strcat(output, temp);
    strcat(output, ".");

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You have to call strcat twice.
    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

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > is this possible in C.
    No.

    I'm not sure if you can in C++, even if you could it would just be the dot operator overloaded.

    Perhaps you want sprintf() ?
    Code:
    sprintf(output, "%s%s%s", output, temp, "blah blah");
    Or your own strcat_lots(), for EXAMPLE:
    Code:
    void strcat_lots(char * destination, ...)
    {
        char * value = NULL;
        va_list vl;
    
        va_start(vl, destination);
    
        while((value = va_arg(vl, char *)) != NULL)
        {
            strcat(destination, value);
        }
        va_end(vl);
    }
    Code:
    strcat_lots(output, temp, "blah blah", NULL);
    Last edited by zacs7; 04-20-2008 at 01:55 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    I'm not sure if you can in C++, even if you could it would just be the dot operator overloaded.
    The dot operator cannot be overloaded in C++. In any case, it is already possible in C++, e.g., by the operator+ overload for std::string.
    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

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Just shows I know nothing about C++

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Perhaps you want sprintf() ?
    Code:
    sprintf(output, "%s%s%s", output, temp, "blah blah");
    Just be aware that if you do something like this:
    Code:
    sprintf(output, "%s%s%s",temp, "blah blah", output);
    then it most likely won't do what you want, because temp and "blah blah" will overwrite the old content of output, and it's being written directly into that pointer before the original value of output is being read, so "strange" things may happen (and it's by no means sure that output has a zero at the end in the middle of a sprintf call, so seriously strange things may happen, including crashing and "weird content" in the output text).

    --
    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. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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