【发布时间】:2020-07-26 09:57:47
【问题描述】:
我有一个字符串 int 映射,我想将它保存到 txt。我有一段时间没用过 c++ 了,我对指针之类的东西很迷茫。
int WriteFile(string fname, map<string, int>* m) {
int count = 0;
if (m->empty())
return 0;
FILE* fp = fopen(fname.c_str(), "w");
if (!fp)
return -errno;
for (map<string, int>::iterator it = m->begin(); it != m->end(); it++) {
fprintf(fp, "%s=%s\n", it->first.c_str(), &it->second);
count++;
}
fclose(fp);
return count;
}
问题是整数被写成垃圾字符。
【问题讨论】:
标签: c++ string visual-studio dictionary fopen