Killing the char on the end of a char array
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
Thanks guys ^_^
-wyrmmage
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

Thanks guys ^_^
-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Code:
unsigned length = strlen(charArray);
if (length > 0)
{
charArray[length - 1] = '\0';
}
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.
thanks man, that worked 
-wyrmmage

-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| char array versus pointer to string constant question | WhatMeWorry | 7 | 6,641 |
Jan 30, 2007 12:26 PM Last Post: bronxbomber92 |
|
| two-dimensional dynamic array, const char* adding | wyrmmage | 2 | 4,355 |
Nov 22, 2006 04:53 PM Last Post: wyrmmage |
|
| InvalWindowRect() is killing me!! | loki74 | 2 | 2,077 |
Jun 29, 2006 03:29 PM Last Post: loki74 |
|
| What... the... heck? Const Char & Chars. | Jones | 6 | 2,968 |
Jun 12, 2006 03:54 PM Last Post: akb825 |
|
| memcpy(stuct array pointer struct array point) | unknown | 22 | 8,829 |
Sep 29, 2005 03:16 PM Last Post: unknown |
|

