鱼C论坛

 找回密码
 立即注册
查看: 3789|回复: 6

关于I/O流的问题,请人帮助。

[复制链接]
发表于 2013-3-21 12:39:03 | 显示全部楼层 |阅读模式
8鱼币
我是初学者,老师要求我们在 D盘中建立一个TXT文件,输入 任意名称和一门课的10次测验成绩。然后要用C++程序读取后原样输出到D盘另一个TXT文件,并且在末尾加上一个double类型的平均分。
我的文件内容: 小明 90 89 91 93 88 87 89 90 92 96.
我的程序是
#include<fstream>
#include<iostream>
using namespace std;
void add_plus_plus(ifstream& in_stream,ofstream& out_stream);
int main()
{
ifstream in;
ofstream out;
in.open("D:\\123123.txt");
out.open("D:\\123321.txt");
add_plus_plus(in,out);
in.close();
out.close();
return 0;
}
void add_plus_plus(ifstream& in_stream,ofstream& out_stream)
{
double sum=0,a,b;
int count=0;
while(in_stream>>a)
{
  sum=sum+a;
  count++;
        }
b=sum/count;
out_stream<<b;
}
但是输出不出来 不知道哪里出错了 , 初学者嘛 。。。  给点帮助啦。

最佳答案

查看完整内容

修改后代码: vs2010
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-21 12:39:04 | 显示全部楼层
修改后代码: vs2010

  1. #include<fstream>
  2. #include<iostream>
  3. #include <string>
  4. using namespace std;
  5. void add_plus_plus(ifstream& in_stream,ofstream& out_stream);
  6. int main()
  7. {
  8.         ifstream in;
  9.         ofstream out;
  10.         in.open("D:\\123123.txt");
  11.         out.open("D:\\123321.txt");
  12.         add_plus_plus(in,out);
  13.         in.close();
  14.         out.close();
  15.         return 0;
  16. }
  17. void add_plus_plus(ifstream& in_stream,ofstream& out_stream)
  18. {
  19.         double sum=0,a,b;
  20.         int count=0;
  21.         string str;
  22.         in_stream>>str;
  23.         out_stream<<str;
  24.         while(in_stream>>a)
  25.         {
  26.                 out_stream<<' '<<a<<' ';
  27.                 sum=sum+a;
  28.                 count++;
  29.         }
  30.         b=sum/count;
  31.         out_stream<<b;
  32. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-21 22:47:22 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-21 22:48:58 | 显示全部楼层
  1. #include <fstream>
  2. using namespace std;
  3. int main () {
  4.         ofstream out("output.txt");
  5.         ifstream in("input.txt");
  6.         out<<in.rdbuf();
  7. }
复制代码
可以用以下程序段来拷贝文件,其余计算平均值的代码还是看你们自己写的.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-22 13:41:35 | 显示全部楼层
我是菜鸟,我自豪
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-22 20:06:58 | 显示全部楼层
已经有正解
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-23 17:25:20 | 显示全部楼层

没发现啊。 都试了啊,没用啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-5 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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