pdCURSES 和 addstr 与字符串的兼容性问题

发布于 2024-11-05 21:07:58 字数 1960 浏览 3 评论 0原文

嘿,所以我试图让 pdCurses 中的 addstr() 与首选字符串类一起工作(Windows Curses),所以我将该函数设为以下 string_to_80char() 函数,该函数应该接受一个字符串并返回一个 80 个字符长的 char大批 (字符数适合控制台中的一行),因为这是 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;
}

Hey so i'm trying to get addstr() in pdCurses to work (windows curses) with the preferred string class so I made the function the following string_to_80char() function, which is supposed to take a string and return an 80 character long char array
(the number of characters fit on one line in the console) since this is the only parameter addstr seems to accept...

However when running the following code i do get the "Just a string" printed but with a random character like an '@' or '4' like 50 spaces after it.....

WHATS THE PROBLEM?? Thanks for the help! =)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-11-12 21:07:58

您的 string_to_80char() 返回一个指向局部变量的指针,并且当函数返回时该变量的生命周期就结束了,因此指针指向垃圾。另外,您没有在返回的字符串末尾放置 '\0' 字符(但除此之外,您返回的内容并不正式存在)。

让调用者提供缓冲区以将 80 char 字符串放入其中(未经测试的示例):

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

Your string_to_80char() is returning a pointer to a local variable and the lifetime of that variable is over when the function returns so the pointer is pointing to garbage. Also, you're not putting a '\0' character at the end of your returned string (but that's besides the point that what you're returning doesn't officially exist anyway).

Have the caller provide the buffer to put the 80 char string into (untested example):

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

Alternatively, allocate the returned buffer on the heap and return that (in which case the caller must free the buffer when they're done with it).

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

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

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

If you're on Windows and don't have strdup(), here you go:

#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;
}
倥絔 2024-11-12 21:07:58

一个问题是您正在返回一个指向 string_to_80char() 中存储在堆栈上的变量的指针。该变量存储在堆栈中:

char charArray[90];

当您从该函数返回时,该变量使用的存储不再有效,并且可能会被重用。 addstr() 的堆栈变量很可能会覆盖相同的存储,因此您的字符串被损坏。

一个简单的修复方法是将 charArray 设为静态,这样它就不会在堆栈上分配:

static char charArray[90];

One problem is that you are returning a pointer to a variable stored on the stack in string_to_80char(). This variable is stored on the stack:

char charArray[90];

When you return from that function, the storage used by this variable is no longer valid, and its likely to be reused. It's likely that addstr()'s stack variables are overwriting this same storage so your string is being corrupted.

A simple fix is to make charArray static so it's not allocated on the stack:

static char charArray[90];
星星的轨迹 2024-11-12 21:07:58
addstr(mesg[0].c_str())

应该就是你所需要的。 PDCurses 是一个 C 库,因此它需要 C 字符串。它们不必是 80 列或任何其他特殊的东西。

或者,创建一个简单的 C++ 包装函数:

int my_addstr(const string &aString)
{
    return addstr(aString.c_str());
}
addstr(mesg[0].c_str())

should be all you need. PDCurses is a C library, so it takes C strings. They don't have to be 80 columns or anything else special.

Alternatively, make a simple C++ wrapper function:

int my_addstr(const string &aString)
{
    return addstr(aString.c_str());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文