文字列が化ける
この辺り、ちゃんとC/C++の文字列を理解していないのが問題だが、下記のような文字列をログファイルに出力する関数を作った場合、
void Common::out_log(FILE* log, const std::string& mes) { fprintf(log, "%s", mes.c_str()); }
コレとは関係ない文字列が、なぜか変化してしまう(文字列自体が化けてしまう場合と、途中で切れてしまう場合)
std::cout << "1 " << cache_in_dir << std::endl; sprintf(mes, "[Info] Loading from Cache Directory (%s) ...\n", cache_in_dir.c_str()); std::cout << "2 " << cache_in_dir << std::endl; Common::out_log(log, mes); std::cout << "3 " << cache_in_dir << std::endl;
1 ./data/L413.xml.cache
2 ./data/L413.xml.cache
[Info] Loading from Cache Directory (./data/L413.xml.cache) ...
3 ./data/L4 <--- 文字列がキレた!!
このout_log関数を下記のように、std::string => char* に変えると、
void Common::out_log(FILE* log, const char* mes) { fprintf(log, "%s", mes); }
正しい文字列が出てくる。
う〜ん、C/C++の知識が足りないorz