一道C++题

cpc1984 2008-08-21 10:54:59
2、编写程序,要求:1)从键盘上输入6个整数放入数组a[6]中(用while循环实现);2)求出数组元素的最大值及数组的平均值,并将各元素、最大值及平均值输出到一文本文件中保存;3)然后打开该文件,读取其中内容并显示在屏幕上。
最大值和平均值我能求出来,怎么把他们保存到txt文件中,在打开文件我就不会了.
...全文
218 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xqls_xqls 2008-08-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wangdeqie 的回复:]
C/C++ code
//对文件操作可以这么写,具体程序偶就不帮你做了,要相信自己能行^_^
#include <stdio.h>
#include <process.h>
void save(int num) //把数据写入文件
{
FILE *fp;
fp=fopen("d:\\1.txt","w");
if(fp==NULL)
{
printf("无法打开!");
exit(0);
}
fwrite(&num,sizeof(int),1,fp);
fclose(fp);
}

int load() //从文件读取数据
{
int num;

[/Quote]

赞同
e_sharp 2008-08-21
  • 打赏
  • 举报
回复
lz为什么不尝试自己写呢?很不理解
zjw6861982 2008-08-21
  • 打赏
  • 举报
回复
楼主想要的
#include <iostream.h>
#include <fstream.h>

void main()
{
int a[6];
int i=0;
int max;
float avg;
char c[128];


fstream f("a.txt", ios::in|ios::out|ios::trunc);

while(i < 6)
{
if(cin>>a[i])
{
i++;
}
else
{
continue;
}
}
max = a[0];
avg = 0;
for(i=0; i<6; i++)
{
if(max < a[i])
{
max = a[i];
}
avg+=a[i];
f<<a[i]<<" ";
}
avg/=6;
f<<endl<<max<<endl<<avg<<endl;
f.seekp(ios::beg);
while(f.getline(c, 128))
{
cout<<c<<endl;
}
}
wangdeqie 2008-08-21
  • 打赏
  • 举报
回复

//对文件操作可以这么写,具体程序偶就不帮你做了,要相信自己能行^_^
#include <stdio.h>
#include <process.h>
void save(int num) //把数据写入文件
{
FILE *fp;
fp=fopen("d:\\1.txt","w");
if(fp==NULL)
{
printf("无法打开!");
exit(0);
}
fwrite(&num,sizeof(int),1,fp);
fclose(fp);
}

int load() //从文件读取数据
{
int num;
FILE *fp;
fp=fopen("d:\\1.txt","rb");
if(fp==NULL)
{
printf("无法打开!");
exit(0);
}
while(!feof(fp))
{
fread(&num,sizeof(int),1,fp);
}
return num;
fclose(fp);
}

void main()
{
int MaxValue=7;
save(MaxValue);
int GetMaxValue=load();
printf("%d\n",GetMaxValue);
}
lily604 2008-08-21
  • 打赏
  • 举报
回复
找本书看看文件部分的内容很简单,楼上例子不错
太乙 2008-08-21
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main()
{
ofstream of("hello.txt");
of<<"hello world";
of.close();
ifstream ifs("hello.txt");
string s;
ifs>>s;
cout<<s;
ifs.close();
system("pause");
}

cwc270 2008-08-21
  • 打赏
  • 举报
回复
参考fopen,fread,fwrite,fclose等函数,
或fstream类等等........
太乙 2008-08-21
  • 打赏
  • 举报
回复
http://www.cppblog.com/lmlf001/archive/2007/08/27/5815.html
c++ 文件操作
专业搬砖填坑 2008-08-21
  • 打赏
  • 举报
回复
同意楼上的,或许这个作业的目的就是要看看你对文件操作掌握的怎样,还是百度或谷歌下,看看其函数的格式,其实不是很难的,跟一般的函数是一样的。
专业搬砖填坑 2008-08-21
  • 打赏
  • 举报
回复
同意楼上的,或许这个作业的目的就是要看看你对文件操作掌握的怎样,还是百度或谷歌下,看看其函数的格式,其实不是很难的,跟一般的函数是一样的。
太乙 2008-08-21
  • 打赏
  • 举报
回复
http://hi.baidu.com/cuifenghui/blog/item/d5169a51190b272543a75b3c.html

c的文件操作
taojian_hhu 2008-08-21
  • 打赏
  • 举报
回复
帮你做其实就是在害你,我觉得csdn的朋友是不会毒害青少年的
wuyu637 2008-08-21
  • 打赏
  • 举报
回复
作业题。。。百度 一下 c++打开文件。
cprime 2008-08-21
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 vanny 的回复:]
基本的文件操作,任何抓一本C++的基础书都有说的。
[/Quote]
vanny 2008-08-21
  • 打赏
  • 举报
回复
基本的文件操作,任何抓一本C++的基础书都有说的。
xianyuxiaoqiang 2008-08-21
  • 打赏
  • 举报
回复
用楼上几位大虾的方法多试试吧。
elegant87 2008-08-21
  • 打赏
  • 举报
回复


//随便写了一个,你看看吧!有问题我们在讨论!
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int SIZE=6;

int main()
{
int a[SIZE];
int i=0;
int max=0;
double sum=0.0, avg=0.0;
cout<<"Enter six data: ";
while(i!=SIZE)
{
cin>>a[i];
++i;
}
for(i=0;i!=SIZE;++i)
{
if(a[i]>max)
max=a[i];
sum+=a[i];
}
avg=sum/SIZE;
//cout<<"MAX="<<max<<endl;
//cout<<"Average="<<avg<<endl;

ofstream outfile;//保存结果
outfile.open("result.txt",ios::out);
if(!outfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
outfile<<"MAX="<<max<<endl<<"Average="<<avg<<endl;
outfile.close();
cout<<"Save the result end!"<<endl;

ifstream readfile;//从文件中读取内容
readfile.open("result.txt",ios::in);
readfile.clear();
if(!readfile.is_open())
{
cerr<<"Can not open the file!"<<endl;
exit(0);
}
string str;
cout<<"Read the file is:"<<endl;
while(readfile>>str)
cout<<str<<endl;
readfile.close();
system("pause");
return 0;
}

64,654

社区成员

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

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