我在 C++ 中使用 fputs 在文件中写入字符串。
fputs (const char*, FILE*);
如果我使用一个简单的语句,例如,
fputs ("information", pFile);
一切正常,“信息”将写入文件。但是如果我写一个类型的变量,
std::vector<std::string>
进入文件,一些非ASCII字符存储在文件中。我是否必须使用一种方法将类型std::vector<std::string>
转换为 fputs 可以识别的格式?
那是正确的,fputs
不明白 astd::vector
在内存中是如何布局的。
当您尝试将 a 传递std::vector
给fputs()
. 您是否尝试通过添加演员或其他东西来解决错误?
采用
fprintf ( pFile, "%s", iterVar.at(currentRun).c_str() )
代替
fprintf ( pFile, "%s", iterVar.at(currentRun).data() )
你不想使用data()
ever 因为它缺少尾随\0
.
(此代码基于提问者对@GregHewgill 的回答的评论,其中iterVar
是类型vector<string>
。)