Thread: Appending charaters to a char*

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Question Appending charaters to a char*

    I'm a newbie to C++. Infact I started to code my first C++ programme today and I need some assistance to get me going.

    I basically want to know, how do I read characters from a text file and append the characters to a char*

    The reason I need a char* is because my constructor requires a char* to instantiate an object. The class has only one private variable of type char* this is because the series of characters (aka. string) will be of variable amount.

    Please help. Thanks.

    Edit: I'm not expecting a complete solution but snippets or guidance.
    Last edited by masterblix; 11-21-2006 at 02:53 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I started to code my first C++ programme today
    IMO, your first C++ program should use the C++ string class. Using char* is C style and is generally not recommended in C++ programs, despite the fact that it is unfortunately taught by many books, tutorials, and instructors.

    As to your question, the answer depends. If the char* already has enough space and you are appending only a single character, you can set the last character to your new character and assign '\0' (the terminating null) to the next spot in the array. If your char* does not have enough space, you need to create a temporary char* with a larger size, then copy over the original string, then append the character as above. Then delete the old array, and assign the temporary pointer to the member variable.

    If you are reading characters from a file, you can read more than one character at a time and append them all at once. If the char* has enough room for the new characters, you can use strcat to concatenate the new string to the old one. If the char* is too small to hold the combined string, you have to reallocate space for it as I mentioned above.

    If you were using the C++ string class, you would just use myString += newChar. That's a big reason why it is preferred.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    C++ file tutorial: http://www.cprogramming.com/tutorial/lesson10.html

    You open a file stream, which you can treat like cin or cout (depending on the type).

    C-string tutorial: http://www.cprogramming.com/tutorial/lesson9.html
    C++ string class tutorial: http://www.cprogramming.com/tutorial/string.html

    Tutorial page: http://cprogramming.com/tutorial.html
    FAQ: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Thumbs up

    Thanks Daved and dwks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Opening a file for binary appending
    By spank in forum C Programming
    Replies: 1
    Last Post: 11-01-2007, 07:34 AM
  3. appending structure bin file
    By voodoo3182 in forum C Programming
    Replies: 8
    Last Post: 08-09-2005, 10:59 AM
  4. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  5. File input and appending to a string
    By LandMonster in forum C Programming
    Replies: 2
    Last Post: 12-04-2001, 01:32 PM