0

'\0'メインから別のローカル関数に送信される文字の配列からデータをコピーしようとしていますが、文字列の最後に追加したにもかかわらず、常に文字化けが表示されます。

これが私のコードの一部です。

for (int i = 0; i < strlen(main) ; i++){
    if (main[i] != ';'){
        local[i] = main[i];  // Copy the characters until `;` isn't found
    } else {
        local[i] = '\0' ;   // If `;` found, null terminate the copied destination.
        break;
    }
}

基本的に、たとえばこのようにメインから送信されるデータ

look;can;you;see;me

My Local-----> 'look??y??>c?Lw?T?w??>c?2+a?'
Actual data in main---> 'look'

上記の例からわかるように、最初の単語だけを取得しようとしていますが、常にゴミが表示されます。理由はわかりません。

編集

これは、私に問題を引き起こしていることを100%確信しているほぼ全体の機能です。

void myFunction(char main[ ]){


   for (int i = 0; i < strlen(main) ; i++){
    if (main[i] != ';'){
        local[i] = main[i];  // Copy the characters until `;` isn't found
    } else {
        local[i] = '\0' ;   // If `;` found, null terminate the copied destination.
        break;
    }
}


        if(main[i] != '\0'){


            int col = 0, row = 0;

            do {
                if(main[i] == ';' || main[i] == '\0') {
                    sending[row++][col] = '\0';
                    col = 0;
                } else {
                    sending[row][col++] = main[i];
                }
            } while(main[i++] != '\0');

        }



    }
4

2 に答える 2

3

; の場合、文字列をゼロで終了することを忘れています。が見つかりません。簡単な修正は for ループを微調整することで、メインの \0 も認識されるようにします。

for (int i = 0; i <= strlen(main); i++) {
于 2012-04-07T06:21:13.573 に答える
1

標準ライブラリがこれを処理します。strchrと の使用strncpy:

size_t length = std::strlen(main);
const char* current_pos = main;
for (int i = 0; ; ++i) {
    size_t chars_remaining = length - std::distance(main, current_pos);
    const char* end_of_field = std::strchr(current_pos, ';');
    if (end_of_field == NULL) {
        std::strncpy(local[i], current_pos, chars_remaining + 1);
        // we're at the end of the input
        break;
    }
    else {
        size_t field_length = std::distance(current_pos, end_of_field);
        std::strncpy(local[i], current_pos, field_length);

        // don't forget to NUL-terminate the string
        local[i][field_length] = '\0';

        // go to next character for the next iteration through loop
        current_pos = end_of_field + 1;
    }
}

std::find個人的には、std::copy( から)が好き<algorithm>です:

size_t length = std::strlen(main);
const char* current_pos = main;
for (int i = 0; ; ++i) {
    size_t chars_remaining = length - std::distance(main, current_pos);
    const char* end_of_field = std::find(current_pos, current_pos + chars_remaining, ';');
    char* output_end = std::copy(current_pos, end_of_field, local[i]);

    // don't forget to NUL-terminate the string
    *output_end = '\0';

    // if we're at the end of main, then we're done;
    // we're at the end if we're on a NUL character
    if (*end_of_field == '\0')
        break;

    // go to next character for the next iteration through loop
    current_pos = end_of_field + 1;
}

私が今までに書いた中で最も美しいコードではありませんが、それは主に C スタイルの文字列とポインター演算を使用しているためであり、元の質問を考えると避けられないように見えます。さらに、オーバーフローに必要なチェックを入れていません。それを行うのは簡単ですが、さらに使いやすくstd::vector<std::string>、標準ライブラリにそれを心配させます。

于 2012-04-08T09:03:04.210 に答える