0

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;
}
4

4 に答える 4

5
char *Result = new char[PathLength+FileLength];

Resultポイントされたデータは終了文字で終了する必要があります\0。または、 が指すような文字列が返されたときに問題が発生しますResult。そう、

Result[PathLength+FileLength-1] = '\0' ;

バッファをオーバーランしないようにしてください。より良いオプションは を使用することstd::stringです。

于 2011-06-09T23:16:15.900 に答える
1

new char1 文字分のスペースを割り当てます。おそらく、文字の配列にスペースを割り当てることを意味します。これはnew char[N]、N が配列のサイズである場合に実行できます (例: new char[40])

于 2011-06-09T23:16:25.777 に答える
0
char *FileName = new char;
char *TexPathAndName = new char;

それはクラッシュするはずです。1文字のバッファを割り当ててから、それらにstrcpyしようとしていますが、明らかにこれらのバッファがすぐにオーバーフローします。また、文字列内の文字の後には、ヌル ターミネータ用に余分なスペースが 1 つ必要であることを覚えておいてください。

于 2011-06-09T23:15:06.997 に答える
0

最善の方法は、 を使用することstd::stringです。

そうでない場合、文字列の長さについては、strlen/wcslen などの関数があります。Windows シェルには、パス操作のための非常に便利な関数もいくつかありますhttp://msdn.microsoft.com/en-us/library/bb773559%28v=vs.85%29.aspx

それらのほとんどは便利で、通常は静的な長さのchar path[MAX_PATH]={}バッファーを操作します。

最大パスは 256 程度であることに注意してください。より深くネストされたフォルダーの場合、いくつかの問題があります。

于 2011-06-09T23:29:06.340 に答える