2011-05-08 30 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()); 
}