[問題] itos strcat等用法

看板C_and_CPP (C/C++)作者 (ㄆ派)時間15年前 (2011/04/06 13:49), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++2008 express 問題(Question): 已經有個文字A 有甚麼內鍵函數可以將變數B(ex:1234)轉成文字,並加在文字A後 ==> A1234 並將之輸出成文.txt檔案的的名稱 ==> A1234.txt 以下是我可能的寫法, 可以執行但跑到一半會跳出一個視窗, 顯示 "Debug error: Program: d:\.......exe File: Run-time check failure #2- stack around the variable 'string' was corrupted." ps. 這個我在VC++6.0的版本run沒有問題 void addTxt( char* fName, int integer = 0 ) { int i = strlen( fName ); char string[5]; itoa( integer, string, 10 ); strcat( string, ".txt" ); strcat( fName+i, string ); } 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.96.144.60

04/06 14:22, , 1F
你認為只有 [5] 夠嗎?
04/06 14:22, 1F

04/06 14:49, , 2F
我都只用到1個而已..
04/06 14:49, 2F

04/06 14:50, , 3F
快去看書上 C 語言字串怎麼存的......
04/06 14:50, 3F

04/06 16:57, , 4F
sprintf(filename,"A%05d.txt",number);
04/06 16:57, 4F

04/06 16:58, , 5F
你開的 string buffer 的確不夠大,akasan 也已指出。
04/06 16:58, 5F
文章代碼(AID): #1Dc_xWhL (C_and_CPP)
文章代碼(AID): #1Dc_xWhL (C_and_CPP)