c++文件操作

从文件读出

向文件写入 

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

int main()
{
vector<string> svec;
string fileName,str;
ifstream inFile;
ofstream outFile;

cin>>fileName;
outFile.open(fileName.c_str(),ios::app);


// outFile<<"sdaferfdfserdsfs"<<endl;
// outFile.close();


inFile.open(fileName.c_str());
if (!inFile)
{
cerr<<":can not open file:"<<fileName<<endl;
inFile.clear();
return 1;
}

while (inFile>>str)
{
svec.push_back(str);
}
inFile.close();
// inFile.clear();
vector<string>::iterator it=svec.begin();

while (it!=svec.end())
{
cout<<*it<<" ";
++it;
}

outFile<<"sdaferfdfserdsfs"<<endl;
outFile.close();
cout<<endl; 

 

posted on 2011-10-09 12:02  kookest  阅读(115)  评论(0)    收藏  举报