0

ねえ、私はpdCursesのaddstr()を優先文字列クラスで動作させようとしているので(windows curses)、この関数を次のstring_to_80char()関数にしました。これは、文字列を受け取り、80文字の長さのcharを返すことになっています。配列(コンソールの1行に収まる文字数)これは、addstrが受け入れるように見える唯一のパラメーターであるため...

ただし、次のコードを実行すると、「Just a string」が出力されますが、「@」や「4」のようなランダムな文字が後に50スペースのように表示されます。

どうしたの??助けてくれてありがとう!=)

#include <curses.h>         /* ncurses.h includes stdio.h */  
#include <string> 
#include <vector>
#include <Windows.h>
#include <iostream>
using namespace std;

char* string_to_80char (const string& aString)
{
    int stringSize = aString.size();
    char charArray[90];

    if(stringSize <= 80)
    {
    for(int I = 0; I< stringSize; I++)
        charArray[I] = aString[I];
    for(int I = stringSize; I < sizeof(charArray); I++)
        charArray [I] = ' ';
    return charArray;
    }

    else
    {
    char error[] = {"STRING TOO LONG"};
    return error;
    }
};


int main()
{
    //   A bunch of Curses API set up:
    WINDOW *wnd;

 wnd = initscr(); // curses call to initialize window and curses mode
 cbreak(); // curses call to set no waiting for Enter key
 noecho(); // curses call to set no echoing

 std::string mesg[]= {"Just a string"};     /* message to be appeared on the screen */
 int row,col;               /* to store the number of rows and *
                     * the number of colums of the screen */
 getmaxyx(stdscr,row,col);      /* get the number of rows and columns */
 clear(); // curses call to clear screen, send cursor to position (0,0)

 string test = string_to_80char(mesg[0]);
 char* test2 = string_to_80char(mesg[0]);
 int test3 = test.size();
 int test4 = test.length();
 int test5 = sizeof(test2);
 int test6 = sizeof(test);

 addstr(string_to_80char(mesg[0]));
 refresh();
 getch();


 cout << endl << "Try resizing your window(if possible) and then run this program again";
  system("PAUSE");
 refresh();
  system("PAUSE");

 endwin();
 return 0;
}
4

3 に答える 3

2

ローカル変数へのポインタstring_to_80char()を返していますが、関数が戻るとその変数の有効期間が終了しているため、ポインタはガベージを指しています。また、返された文字列の最後に文字を入れていません'\0'(ただし、返されるものが公式には存在しないという点を超えています)。

呼び出し元に、80char文字列を挿入するためのバッファーを提供してもらいます(テストされていない例)。

char* string_to_80char (const string& aString, char* buf, size_t bufSize)
{
    int stringSize = aString.size();
    enum {
        max_buf_size = 81;  /* 80 plus the '\0' terminator */
    };

    bufSize = (bufSize < max_buf_size) ? bufSize : max_buf_size; 

    if (stringSize+1 < bufSize) {
        return NULL;  /* or however you want to handle the error */
    }

    /* we know the buffer is large enough, so strcpy() is safe */
    strcpy( buf, aString.c_str());

    return buf;
};

または、返されたバッファをヒープに割り当てて返します(この場合、呼び出し元は、バッファの使用が終了したときにバッファを解放する必要があります)。

char* string_to_80char (const string& aString)
{
    int stringSize = aString.size();

    if(stringSize <= 80)
    {
        return strdup(aString.c_str());
    }

    return strdup("STRING TOO LONG");
};

Windowsを使用していて、持っていない場合はstrdup()、次のようにします。

#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* 
 * public domain strdup()
 */

char* strdup( char const* s)
{
   size_t siz = 0;
   char* result = NULL;
   assert( s);

   siz = strlen( s) + 1;
   result = (char*) malloc( siz);

   if (result) {
       memcpy( result, s, siz);
   }

   return result;
}
于 2011-05-08T20:34:33.417 に答える
0

1つの問題は、string_to_80char()でスタックに格納されている変数へのポインターを返すことです。この変数はスタックに格納されます。

char charArray[90];

その関数から戻ると、この変数によって使用されているストレージは無効になり、再利用される可能性があります。addstr()のスタック変数がこの同じストレージを上書きしている可能性があるため、文字列が破損しています。

簡単な修正は、charArrayを静的にして、スタックに割り当てられないようにすることです。

static char charArray[90];
于 2011-05-16T17:13:01.677 に答える
0
addstr(mesg[0].c_str())

必要なのはそれだけです。PDCursesはCライブラリであるため、C文字列を使用します。80列などの特別なものである必要はありません。

または、単純なC++ラッパー関数を作成します。

int my_addstr(const string &aString)
{
    return addstr(aString.c_str());
}
于 2014-03-01T22:40:50.153 に答える