Killing the char on the end of a char array

Moderator
Posts: 373
Joined: 2006.08
Post: #1
This is more of a basic C++ question than anything to do with Mac programming:
I have a dynamic char array (char* charArray = new char[6]). Then I read some text into it with the fscanf() function. Now, I just need to chop off the last character in this array, but it wont let me do: charArray[(last letter)] = '', and it doesn't work by setting it to NULL, either...how do I do this?
This seems like it would be a fairly common question, but, once again, after drudging through the material I found of Google, I didn't find anything useful, so I thought that I would post here Wink
Thanks guys ^_^
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
unsigned length = strlen(charArray);
if (length > 0)
{
    charArray[length - 1] = '\0';
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
If you're in C++, though, you should seriously consider using std::string instead of char arrays. It's much easier to use, and much safer, too.
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #4
thanks man, that worked Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  char array versus pointer to string constant question WhatMeWorry 7 7,470 Jan 30, 2007 12:26 PM
Last Post: bronxbomber92
  two-dimensional dynamic array, const char* adding wyrmmage 2 4,942 Nov 22, 2006 04:53 PM
Last Post: wyrmmage
  InvalWindowRect() is killing me!! loki74 2 2,403 Jun 29, 2006 03:29 PM
Last Post: loki74
  What... the... heck? Const Char & Chars. Jones 6 3,579 Jun 12, 2006 03:54 PM
Last Post: akb825
  memcpy(stuct array pointer struct array point) unknown 22 11,030 Sep 29, 2005 03:16 PM
Last Post: unknown