2016-05-27 67 views
0

我试图在我的游戏中制作排行榜,但遇到了一个我无法弄清楚的问题。 我有我的文本文件得分的名称和整数字符串。我尝试将它们复制到ALLEGRO_USTR以在屏幕上显示它。使用al_ustr_newf复制字符串Allegro 5

当我使用al_ustr_newf("%s", name1)时,它会复制一些随机标志。

fstream file2; 
file2.open("leaderboard.txt", ios_base::in); 

string name1; 
string name2; 
string name3; 
int temp_score1; 
int temp_score2; 
int temp_score3; 

file2 >> name1 >> temp_score1; 
file2 >> name2 >> temp_score2; 
file2 >> name3 >> temp_score3; 

ALLEGRO_USTR * ustr_name1 = NULL; 
ustr_name1 = al_ustr_newf("%s", name1); 

也许还有另一种方法来复制字符串在快板5?

回答

0

al_ustr_newf reference

创建使用printf风格的格式字符串的新字符串。

注:

的 “%S” 符需要C字符串参数,而不是ALLEGRO_USTRs。因此,要将ALLEGRO_USTR作为参数传递,您必须使用al_cstr,并且它必须以NUL结尾。如果字符串包含嵌入的NUL字节,则该字节以后的所有内容都将被忽略。

话虽这么说,al_ustr_newf("%s", name1);是不确定的行为,通过你的堆栈变量迭代,直到它找到一个NUL字节。 std::string的地址几乎不会与实际缓冲区的地址相同。

使用al_ustr_newf("%s", name1.c_str());,就像你必须使用printfstd::string一样。

+0

非常感谢!它现在工作,因为我希望它:) – P3piK