C++问题求解答
看essentialc++有个问题:#include<iostream>#include<fstream>#include<algorithm>#include<stri...
看essential c++有个问题:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
if(!in_file)
{
cerr<<"opps! unable to open input file\n";
return -1;
}
ofstream out_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.sort");
if(!out_file)
{
cerr<<"opps! unable to open output file\n";
return -2;
}
string word;
vector<string> text;
while(in_file>>word)
{
text.push_back(word);
}
int ix;
cout<<"unsorted text:\n";
for(ix=0;ix<text.size();++ix)
cout<<text[ix]<<' ';
cout<<endl;
sort(text.begin(),text.end());
out_file<<"sorted text:\n";
for(ix=0;ix<text.size();++ix)
out_file<<text[ix]<<' ';
out_file<<endl;
return 0;
}
怎么我运行没有输入的,已经有文件了。 展开
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
if(!in_file)
{
cerr<<"opps! unable to open input file\n";
return -1;
}
ofstream out_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.sort");
if(!out_file)
{
cerr<<"opps! unable to open output file\n";
return -2;
}
string word;
vector<string> text;
while(in_file>>word)
{
text.push_back(word);
}
int ix;
cout<<"unsorted text:\n";
for(ix=0;ix<text.size();++ix)
cout<<text[ix]<<' ';
cout<<endl;
sort(text.begin(),text.end());
out_file<<"sorted text:\n";
for(ix=0;ix<text.size();++ix)
out_file<<text[ix]<<' ';
out_file<<endl;
return 0;
}
怎么我运行没有输入的,已经有文件了。 展开
2个回答
展开全部
你输入的是这个文件流
ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
你要自己输入文件的话可以这样改。
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout<<"input the filenam:"<<endl;
string filename;
cin>>filename;
ifstream in_file(filename.c_str());
//ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
if(!in_file)
{
cerr<<"opps! unable to open input file\n";
return -1;
}
ofstream out_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.sort");
if(!out_file)
{
cerr<<"opps! unable to open output file\n";
return -2;
}
string word;
vector<string> text;
while(in_file>>word)
{
text.push_back(word);
}
int ix;
cout<<"unsorted text:\n";
for(ix=0;ix<text.size();++ix)
cout<<text[ix]<<' ';
cout<<endl;
sort(text.begin(),text.end());
out_file<<"sorted text:\n";
for(ix=0;ix<text.size();++ix)
out_file<<text[ix]<<' ';
out_file<<endl;
return 0;
}
ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
你要自己输入文件的话可以这样改。
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout<<"input the filenam:"<<endl;
string filename;
cin>>filename;
ifstream in_file(filename.c_str());
//ifstream in_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.txt");
if(!in_file)
{
cerr<<"opps! unable to open input file\n";
return -1;
}
ofstream out_file("C:\\Documents and Settings\\Administrator\\My Documents\\text.sort");
if(!out_file)
{
cerr<<"opps! unable to open output file\n";
return -2;
}
string word;
vector<string> text;
while(in_file>>word)
{
text.push_back(word);
}
int ix;
cout<<"unsorted text:\n";
for(ix=0;ix<text.size();++ix)
cout<<text[ix]<<' ';
cout<<endl;
sort(text.begin(),text.end());
out_file<<"sorted text:\n";
for(ix=0;ix<text.size();++ix)
out_file<<text[ix]<<' ';
out_file<<endl;
return 0;
}
追问
照着做啊,根本没给输入的机会就直接输出了。
我在想这个:
while(in_file>>word)
{
text.push_back(word);
}
是不是不能进行 写 操作,加了方式还是不行,额,我还是再想想。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询