2011-05-08 34 views
0

嘿所以我试图让ptrCurses中的addstr()与首选的字符串类工作(窗口诅咒),所以我做了函数以下string_to_80char()函数,应该取一个字符串并返回一个长度为80个字符的字符数组 (字符数适合控制台中的一行),因为这是唯一的参数addstr似乎接受...pdCURSES和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; 
} 

回答

2

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

的一个问题是,你是返回一个指针存储在堆栈上的string_to_80char变量( )。这个变量保存在栈上:

char charArray[90]; 

当您从函数返回,这个变量所使用的存储不再有效,其可能被重复使用。很可能addstr()的堆栈变量覆盖了这个相同的存储,所以你的字符串被破坏了。

一个简单的解决方法是让charArray静态的,因此它不是在栈上分配:

static char charArray[90]; 
0
addstr(mesg[0].c_str()) 

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

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

int my_addstr(const string &aString) 
{ 
    return addstr(aString.c_str()); 
}