找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1801|回复: 1

文件问题

[复制链接]
发表于 2009-11-4 01:10:37 | 显示全部楼层 |阅读模式 IP:江苏扬州
如何将a.txt文件的内容
平均分配到,
b.txt和 c.txt中
发表于 2009-11-4 01:10:38 | 显示全部楼层 IP:江苏扬州
1.把a.txt的文件内容放进vector<string> svec中
2.分别读取svec的一半内容放到 b.txt和 c.txt中
  1. #include <fstream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. int main()
  7. {
  8. ifstream infile("D:\\a.txt",ios::in);
  9. if(!infile)
  10. {
  11. cout<<"can't open a.txt"<<endl;
  12. exit(-1);
  13. }
  14. vector<string> svec;
  15. string word;
  16. while(infile>>word)
  17. svec.push_back(word);
  18. infile.close();
  19. ofstream outfile1("D:\\b.txt");
  20. if(!outfile1)
  21. {
  22. cout<<"can't open b.txt"<<endl;
  23. exit(-1);
  24. }
  25. vector<string>::const_iterator iter = svec.begin();
  26. int ct = 0;
  27. while(iter != svec.begin()+svec.size()/2)
  28. {
  29. ct++;
  30. if(ct == 15)
  31. outfile1<<endl;
  32. outfile1<<*iter<<' ';
  33. iter++;
  34. }
  35. outfile1.close();
  36. ofstream outfile2("D:\\c.txt");
  37. if(!outfile2)
  38. {
  39. cout<<"can't open c.txt"<<endl;
  40. exit(-1);
  41. }
  42. iter = svec.begin()+svec.size()/2;
  43. ct = 0;
  44. while(iter != svec.end())
  45. {
  46. ct++;
  47. if(ct == 15)
  48. outfile2<<endl;
  49. outfile2<<*iter<<' ';
  50. iter++;
  51. }
  52. outfile2.close();
  53. return 0;
  54. }


复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 01:32 , Processed in 0.222783 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表