mat*_*ath 3 c++ string file-io file text-files
我在阅读时发现了一些好的结果,例如:使用迭代器和预分配读取txt,或者读入容器.所以我想知道如何将最优雅的std :: string写入文件?
编辑:读取时我可以通过seek和tellg预先为字符串分配空间,因为我知道字符串的大小,我怎么能告诉文件系统我要写多少?
这是关于如何输出a的一个小例子std::string,但你应该真正阅读fstream
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char *argv[])
{
std::string s("writemeout");
std::ofstream outfile("output.txt");
if(!outfile.is_open()) {
std::cerr << "Couldn't open 'output.txt'" << std::endl;
return -1;
}
outfile << s << std::endl;
outfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6660 次 |
| 最近记录: |