| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 333 人关注过本帖
标题:一个文件程序,里面有点小问题
只看楼主 加入收藏
lyj23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:140
注 册:2010-10-31
结帖率:86.21%
收藏
已结贴  问题点数:10 回复次数:5 
一个文件程序,里面有点小问题
我用DEV C++
程序代码:
#include <fstream>
#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
int a[10],max,i,order;
ofstream outfile("exercise_4.txt",ios::out|ios::trunc);
if(!outfile)
{

 cerr<<"can't open this file"<<endl<<"open error"<<endl;

 getchar();

 exit(0);
}
cout<<"put 10 number in this:"<<endl;
for(i=0;i<10;i++)

 {cin>>a[i];

 outfile<<a[i]<<" ";}
outfile.close();
//上半部分
ifstream infile("exercise_4.txt",ios::in);
if(!infile)
{

 cerr<<"can't open this file"<<endl<<"open error"<<endl;

 getchar();

 exit(0);
}
for(i=0;i<10;i++)
{

 infile>>a[i];

 cout<<a[i]<<" "<<endl;
}
cout<<endl;
max=a[0];
for(i=1;i<10;i++)

 if(a[i]>max)
  {
   max=a[i];
   order=i;
  }
cout<<"max="<<max<<endl;
cout<<"order="<<order<<endl;
getch();  //这里我写getch();就不会在执行完上半部分后闪屏而过,用getchar();为什么就会闪屏?
infile.close();   //这两个函数不是差不多吗,只不过显示和不显示。。
cout<<"The porgram is end."<<endl;
return 0;
}
2011-05-07 15:19
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
我还真不懂!

   唯实惟新 至诚致志
2011-05-07 15:29
tisyang
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:132
专家分:737
注 册:2011-5-7
收藏
得分:2 
程序代码:
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
  int a[10];
  cout << "Please input 10 interger: \n";
  for(int i=0; i<10; i++)
    {
      cin >> a[i];
    }
  
  ofstream outfile("exercise_4.txt", ios::out| ios::trunc);
  if(!outfile)
    {
      cerr << "Can't create file!\n";
      return 1;
    }

  for(int i=0; i<10; i++)
    {
      outfile << a[i] <<" ";
    }
  outfile.close();
  // this is the end of first part.
  
  ifstream infile("exercise_4.txt", ios::in);
  if(!infile)
    {
      cerr << "Can't open file!\n";
      return 1;
    }
  int b[10];
  for(int i=0; i<10; i++)
    {
      infile >> b[i]; 
    }
  infile.close();

  int max=b[0];
  int index = -1;
  for(int i=1; i<10; i++)
    if(b[i] > max)
      {
        max = b[i];
        index = i;
      }
  cout << "max= "<< max << endl;
  cout << "index= "<< index+1 << endl;
  cout << "This program ends." << endl;
  return 0;
}

大意应该是一样的。
for(i=0;i<10;i++)
{cin>>a[i];
outfile<<a[i]<<" ";}
这样的块既做输入,又做输出功能,最好不要有这样的写法。

C++ 用无参数构造函数生成对象时候请勿在构造函数后添加无用的那一对括号,否则有可能会被当成函数声明而忽略,嗯,栈上构建的时候就是这样。
2011-05-07 17:45
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:5 
getchar
This is a standard function that gets a character from the stdin.
getche
This is a nonstandard function that gets a character from the keyboard, echoes to screen.
你说的闪屏是什么、是指等待输入吗。

离恨恰如春草,更行更远还生。
2011-05-07 20:21
lyj23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:140
注 册:2010-10-31
收藏
得分:0 
我说的闪屏就是DEVC++程序结束后没有看的时间!
直接一闪就没了!
写getchar()||getch()是为了等待键入字符!
为什么有时候getchar()写了照样闪,getch()就不闪了?
2011-05-08 08:28
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:3 
看4L的解释,另有一个函数你可能会有用,fflush()/、最后一个回车符还残留在输入缓冲区中。

离恨恰如春草,更行更远还生。
2011-05-08 11:28
快速回复:一个文件程序,里面有点小问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013868 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved