pdCURSES和addstr兼容字符串问题 [英] pdCURSES and addstr compatibility with strings problems

查看:236
本文介绍了pdCURSES和addstr兼容字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

但是当运行下面的代码,我得到只是一个字符串打印,但随机字符像一个'@'或'4',像50之后的空格.....

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


推荐答案

> string_to_80char()返回一个指向局部变量的指针,当函数返回时,变量的生命周期结束,因此指针指向垃圾。此外,你不是在返回的字符串的末尾放置一个'\0'字符(但这除了你返回的东西不正式

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).

让调用者提供缓冲区来将80 char 字符串插入(未测试的例子):

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

如果你在Windows上,并且没有 strdup / code>,这里你去:

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

这篇关于pdCURSES和addstr兼容字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆