c++ 文件读写
1 文件写
#include<fstream>
using namespace std;
int main()
{
ofstream out("w.txt");
if(out.is_open())
{
out << "this is a line" << endl;
out << "this is anthor line";
out.close();
}
return 0;
}
2 文件读 #include<iostream> #include<fstream> using namespace std; int main() { char buff[256]; ifstream in("w.txt"); while(!in.eof()) { in.getline(buff, 100); cout << buff << endl; } return 0; }
按行读取
std::ifstream fin(_path.c_str());
while (!fin.eof()) {
std::string line;
getline(fin, line);
//trim(line);
if (site.empty()) {
continue;
}
_sites.insert(site);
}
//std::cout << "sites num: " << _sites.size() << std::endl;
fin.close();