hmmm... I think you need a class like this one I'm posting or learn about dynamic memory, static char buffers, and the string lib or something.
My Class is a home brew string class whos advantage is in its simplicity. Correct? Many will say Bad Form! Does it Work? Yup. Easy? Yup. Lean? YUP... Perfect? Well.. doesn't crash unless you mess up terribly. Its been working for in MILES and MILES of code.
jfc_string.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
//=============================================================================
class JFC_STRING{
//=============================================================================
public:
char *s;char chNull;
unsigned int uiLength;
JFC_STRING();
~JFC_STRING();
void Clear(void);
void Clear(int p_NewLength);// allows making a buffer instantly.
void ConCat(const char *p_char);
void Set(const char *p_char);
char *Get(void);
unsigned int Length(void);
bool EqualCS(char *p_szCompareThis); // Case Sensitive
bool Equal(char *p_szCompareThis);
//function Replace(source$,search$,key$)
//Function Misc_strip_path(f$)
//Function Remove_End(f$)
//function strInstr(source$,search$)
};
//=============================================================================
jfc_string.cpp
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#include "jfc_string.h"
#include "string.h"
//=============================================================================
JFC_STRING::JFC_STRING(){
//=============================================================================
this->s=0;
chNull='';
this->Clear();
};
//=============================================================================
//=============================================================================
JFC_STRING::~JFC_STRING(){
//=============================================================================
if(this->s!=0){
delete this->s;
};
};
//=============================================================================
//=============================================================================
void JFC_STRING::Clear(void){
//=============================================================================
if(this->s!=0){
delete this->s;
};
this->uiLength=0;
this->s=new char[this->uiLength+1];
this->s[this->uiLength]='';
};
//=============================================================================
//=============================================================================
void JFC_STRING::Clear(int p_NewLength){
//=============================================================================
if(this->s!=0){
delete this->s;
};
this->uiLength=p_NewLength;
this->s=new char[this->uiLength+1];
this->s[this->uiLength]='';
int i;
for(i=0;i<this->uiLength;i++){
this->s[i]='';
};
};
//=============================================================================
//=============================================================================
void JFC_STRING::ConCat(const char *p_char){
//=============================================================================
char *st;
this->uiLength=(unsigned int)(strlen(p_char)+this->uiLength);
st=new char[this->uiLength+1];
st[0]=chNull;
strcat(st,this->s);
strcat(st,p_char);
delete this->s;
this->s=new char[this->uiLength+1];
strcpy(this->s,st);
delete st;
this->s[this->uiLength]='';
};
//=============================================================================
//=============================================================================
void JFC_STRING::Set(const char *p_char){
//=============================================================================
this->Clear();
this->ConCat(p_char);
};
//=============================================================================
//=============================================================================
char *JFC_STRING::Get(void){
//=============================================================================
if(this->s!=0){
return this->s;
}else{
return 0;
};
};
//=============================================================================
//=============================================================================
unsigned int JFC_STRING::Length(void){
//=============================================================================
return this->uiLength;
};
//=============================================================================
//=============================================================================
bool JFC_STRING::EqualCS(char *p_szCompareThis){
//=============================================================================
return (strcmp(this->s,p_szCompareThis)==0);
};
//=============================================================================
//=============================================================================
bool JFC_STRING::Equal(char *p_szCompareThis){
//=============================================================================
return (strcmpi(this->s,p_szCompareThis)==0);
};
//=============================================================================
Then you can do something like this:
JFC_STRING *t=new JFC_STRING();
t->Set("Hello Mom");
t->ConCat(" I Love You");
dbPrint(t->Get()); // dbPrint(t->s()); would work too.
There isn't a lot of things in that class so its to learn and I find I USUALLY don't need more than the basics in game dev.
And if you take these two files and include them...
jfc_common.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
//============================================================================
//============================================================================
namespace JFC{
//============================================================================
extern char CHAR1K[1024];
extern float Interpolate(float x0, float x1, float x , float y0, float y1);
};
//============================================================================
jfc_common.cpp
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#include "jfc_common.h"
//============================================================================
// begin JFC_COMMON
//============================================================================
namespace JFC{
char CHAR1K[1024];
float Interpolate(float x0, float x1, float x , float y0, float y1);
};
//============================================================================
//============================================================================
// This Function Interpolates Linear Data to find to numerically
// "Guess" what the missing data is.
// The Formula Semantics are to Find "X", but by swapping the parameters
// you can find Your own "Y" just as easily.
float JFC::Interpolate(float x0, float x1, float x , float y0, float y1){
//============================================================================
return y0+((x-x0)*((y1-y0)/(x1-x0)));
};
//============================================================================
//============================================================================
// end JFC_COMMON
//============================================================================
(Aside from a global interpolate routine you don't need now) you'll have a GLOBAL 1024 byte buffer you can use in your code anywhere. Example:
sprintf(JFC::CHAR1K, "This is a String...Um an Integer here:%i",iMyInt);
dbPrint(JFC::CHAR1K);
[edit] Fixed filenames and some typos I had in bold... [/edit]