关于C++输入流和输出流

快乐的逗比 2011-06-02 09:04:40
我编了好长时间,越来越糊涂,求助高手帮帮我,谢谢。帮忙写写代码。我之前写过类似的,但不是输入流输出流。



用C++(的文件操作)编写一程序从文本文件中读入若干个字符串(每个串长度不超过80个字符),将字符串按字典序(从小到大)排序,结果输出到另一个正文文件中。希望此程序能处理任意多个字符串。
...全文
120 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐的逗比 2011-06-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dizuo 的回复:]
按照字符串遍历文件存到vector中,排序输出即可。
[/Quote]
还是自己好好想想吧。。初学者不懂这些
無_1024 2011-06-02
  • 打赏
  • 举报
回复

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

using namespace std;

int main()
{
vector<string> str;
ifstream infile("test.txt");
ofstream outfile("output.txt");
if( !infile )
{
cerr << "file open error!" << endl;
exit(0);
}
string s;
infile >> s;
while( !infile.eof() )
{
str.push_back(s);
infile >> s;
}
sort(str.begin(),str.end());
for(vector<string>::iterator iter = str.begin(); iter != str.end(); iter++ )
{
//cout << *iter << endl;
outfile << *iter << '\n';
}
return 0;
}

快乐的逗比 2011-06-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dizuo 的回复:]
lz 结贴给分~

C/C++ code
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
ifstream inFile("111.txt");
……
[/Quote]
我看不懂,初学者真悲剧。。。
快乐的逗比 2011-06-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hnuqinhuan 的回复:]
C/C++ code

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

using namespace std;

int main()
{
vector<string> str;
……
[/Quote]
我也找到了,可是这个我看不懂啊。我初学输入流输出流。。
ryfdizuo 2011-06-02
  • 打赏
  • 举报
回复
lz 结贴给分~
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
ifstream inFile("111.txt");
vector<string> svec;
copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(svec));

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

copy(svec.begin(), svec.end(), ostream_iterator<string>(cout,"\n"));

inFile.close();

system("PAUSE");
return 0;
}
文件内容:
fopen example One Two Three Four Five Six Seven
输出:
Five
Four
One
Seven
Six
Three
Two
example
fopen
请按任意键继续. . .
無_1024 2011-06-02
  • 打赏
  • 举报
回复

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

using namespace std;

int main()
{
vector<string> str;
ifstream infile("test.txt");
if( !infile )
{
cerr << "file open error!" << endl;
exit(0);
}
string s;
infile >> s;
while( !infile.eof() )
{
str.push_back(s);
infile >> s;
}
sort(str.begin(),str.end());
for(vector<string>::iterator iter = str.begin(); iter != str.end(); iter++ )
{
cout << *iter << endl;
}
return 0;
}

Jim_King_2000 2011-06-02
  • 打赏
  • 举报
回复
如果不考虑效率的话,基本上可以这样。

1、用一对迭代器构造字符串数组。

ifstream text_file("filename");
istream_iterator<string> begin(text_file), end;
vector<string> data(begin, end);

2、排序。

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

3、输出。

ofstream output_file("filename");
ostream_iterator<string> output_begin(output_file);
copy(data.begin(), data.end(), output_begin);

ryfdizuo 2011-06-02
  • 打赏
  • 举报
回复
按照字符串遍历文件存到vector中,排序输出即可。
快乐的逗比 2011-06-02
  • 打赏
  • 举报
回复
没人么?

64,646

社区成员

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

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