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;
}
怎么我运行没有输入的,已经有文件了。 展开
展开全部
你输入的是这个文件流
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);
}
是不是不能进行 写 操作,加了方式还是不行,额,我还是再想想。
TableDI
2024-07-18 广告
2024-07-18 广告
VLOOKUP是Excel中用于垂直查找的函数,其基本用法包括四个参数:1. 查找值:即在数据表首列中需要搜索的值。2. 数据表:包含查找值的单元格区域或数组。3. 返回值所在列数:指定返回查询区域中第几列的值。4. 查找方式:选择精确匹配...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询