0

嘿,所以我试图让 pdCurses 中的 addstr() 与首选字符串类一起工作(windows curses),所以我将该函数设为以下 string_to_80char() 函数,该函数应该接受一个字符串并返回一个 80 字符长的字符数组(控制台中一行的字符数),因为这是 addstr 似乎接受的唯一参数...

但是,当运行以下代码时,我确实打印了“只是一个字符串”,但后面有一个随机字符,如“@”或“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

一个问题是您正在返回一个指针,该指针指向存储在 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 回答