LPSTR 型変数を分割し、それを char 配列 (char*) にアタッチする C++ 関数があります。例:
this->XMeshTexturePath = FindTexturePath(XMeshTexturePath,d3dxMaterials[i].pTextureFilename);
//the value of XMeshTexturePath is: Models\\Textures\\
//the value of d3dxMaterials[i].pTextureFilename is: BlaBlaBla\\BlaBla\\Cyrex.x
//The Result(XMeshTexturePath) should be like this:"Models\\Textures\\Cyrex.x"
これは私が書こうとしている関数です:
int FindTextLength(char* Text){
int length
h=0; for(int i=0;i
char* FindTexturePath( char* TexturePath ,LPSTR FileNameToCombine){
int FileLength=0;
int PathAndFileLength=0;
char *FileName = new char;
char *TexPathAndName = new char;
strcpy(TexPathAndName, FileNameToCombine);
PathAndFileLength = FindTextLength(TexPathAndName);
for(int i=0; i<PathAndFileLength; i++){
if( TexPathAndName[i] != NULL){
if(TexPathAndName[i] != '\\'){
FileName[FileLength] = TexPathAndName[i];
FileLength++;
}
else
FileLength = 0 ;
}else break;
}
int PathLength = FindTextLength(TexturePath);
char *Result = new char;
//==============>> // I also tryed this:char *Result = new char[PathLength+FileLength];
//==============>> // char *Result = new char();
for(int i=0; i<PathLength; i++){
if( TexturePath[0] != NULL){
Result[i] = TexturePath[i];
}
else break;
}
for(int i=0; i<FileLength; i++){
if( FileName[0] != NULL){
Result[PathLength + i] = FileName[i];
}
else break;
}
return **Result**; // The Problem is here It should be like this:
// "Models\\Textures\\Cyrex.x"
// But I'm taking one of these as result:
// "Models\\Textures\\Cyrex.x{"
// "Models\\Textures\\Cyrex.xu"
// "Models\\Textures\\Cyrex.xY"
// The last character is random... 8O(
}
実際にはそれほど悪くはありません。問題は、char 配列 ( char *Result = new char; ) を宣言するときです。長さがどれくらいかは関係ありません。最終結果 ( Result )の最後に余分な文字を追加しています。本当にここで立ち往生しています。アイデアや提案があれば教えてください。アドバイスと回答をありがとう。
ソリューションは、関数の最後にこれを追加しています:
Result[i] = TexturePath[i];
}
else break;
}
for(int i=0; i<FileLength; i++){
if( FileName[0] != NULL){
Result[PathLength + i] = FileName[i];
}
else break;
}
Result[PathLength+FileLength] = '\0' ; // This part is resloving the problem.
// **Thanks for helps**.
return Result;
}