想知道.到低那个地方错了.

harderror 2012-07-16 12:56:01

#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main(){
ifstream in_file("d:\\myfile.txt");
if (!in_file)
{
cerr << "oops! out_file\n" ;
return -1;
}
ofstream out_file("d:\\myfile.txt");
if (!out_file)
{
cerr << "oops! out_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] << "\t";
cout << endl;

sort(text.begin(),text.end());

out_file << "sorted text: \n";
for (ix=1;ix<text.size();++ix)
out_file << text[ix] << "\t";
out_file << endl;
system("pause");
return 0;
}
...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
harderror 2012-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:

引用 1 楼 的回复:

为什么要用infile和outfile同时对一个文件??这个是不可能的。必定有一个会打开文件失败的

这个...?要怎样改?


这样改:
C/C++ code

#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>……
[/Quote]

谢谢你了,我明白了!
pathuang68 2012-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

为什么要用infile和outfile同时对一个文件??这个是不可能的。必定有一个会打开文件失败的

这个...?要怎样改?
[/Quote]

这样改:

#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main()
{
ifstream in_file("d:\\myfile.txt");
if (!in_file)
{
cerr << "oops! in_file\n" ;
return -1;
}

string word;
vector<string> text;

while (in_file >> word)
{
text.push_back(word);
}
in_file.close();

int ix;
cout << "unsorted text: \n";

for (ix=0;ix<text.size();++ix)
cout << text[ix] << "\t";
cout << endl;

sort(text.begin(),text.end());


ofstream out_file("d:\\myfile.txt");
if (!out_file)
{
cerr << "oops! out_file\n" ;
return -2;
}
out_file << "sorted text: \n";
for (ix=1;ix<text.size();++ix)
out_file << text[ix] << "\t";
out_file << endl;

out_file.close();
system("pause");

return 0;
}
harderror 2012-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

为什么要用infile和outfile同时对一个文件??这个是不可能的。必定有一个会打开文件失败的
[/Quote]
这个...?要怎样改?
W170532934 2012-07-16
  • 打赏
  • 举报
回复
为什么要用infile和outfile同时对一个文件??这个是不可能的。必定有一个会打开文件失败的

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧