| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:读入文件的问题
只看楼主 加入收藏
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
 问题点数:0 回复次数:8 
读入文件的问题

#include<iostream>
#include<fstream>
#include<cstdlib>

const int SIZE=60;

int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;

cout<<"Enter name of data file:";
cin.getline(filename,SIZE);
inFile.open(filename);
if(!inFile.is_open())
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";

}

double value;
double sum=0.0;
int count=0;

inFile>>value;
while(inFile.good())
{
++count;
sum+=value;
inFile>>value;
}

if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mimatch.\n";
else
cout<<"Input terminated for unknow reasons.\n";
if(count==0)
cout<<"No data procesed.\n";
else
{
cout<<"Item read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<sum/count<<endl;
}

system("pause");
return 0;


}

我已经在我的目录下建立了相应我要输入的文件名并存放了数据
但是运行后系统总是提醒我或是无法打开文件
也就是inFile.is_open()返回FALSE
为什么呢????????

搜索更多相关主题的帖子: 文件 
2006-09-25 15:25
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

c:\\data.txt
not c:\data.txt.
要多加一个\


2006-09-25 18:46
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
还是不对
2006-09-25 22:14
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
怎么没人拉???
2006-09-25 22:40
wyg_616
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-9-24
收藏
得分:0 

我看了一下,只需把你的程序里面的
while(inFile.good())
{
++count;
sum+=value;
inFile>>value;
}
的条件该为inFile就可以了.
还要注意你的路径的输入格式(后缀).
比如:
f:\qw.txt


胸有成竹,而面如平湖这可拜上将军
2006-09-26 18:56
wyg_616
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-9-24
收藏
得分:0 

改过之后的程序如下:
#include<iostream>
#include<fstream>
#include<cstdlib>

const int SIZE=60;

int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;

cout<<"Enter name of data file:";
cin.getline(filename,SIZE);
inFile.open(filename);
if(!inFile.is_open())
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";

}

double value;
double sum=0.0;
int count=0;
//cout<<"Ener the value:";
inFile>>value;
while(inFile)
{
++count;
sum+=value;
inFile>>value;
}

if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mimatch.\n";
else
cout<<"Input terminated for unknow reasons.\n";
if(count==0)
cout<<"No data procesed.\n";
else
{
cout<<"Item read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<sum/count<<endl;
}

system("pause");
return 0;


}


胸有成竹,而面如平湖这可拜上将军
2006-09-26 18:58
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
了..竟然还是不行
2006-09-26 19:11
flypampas
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-16
收藏
得分:0 

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
const int SIZE=60;

int main()
{

string filename;//直接把文件名定义成string ,不是更简单吗?!
ifstream inFile;
cout<<"Enter name of data file:";
cin>>filename;
inFile.open(filename.c_str());//把C++风格的字符串转换成C风格的字符串。打开的文件名一定要是C风格的字符串

if(!inFile)
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";

}

double value;
double sum=0.0;
int count=0;

inFile>>value;
while(inFile.good())
{
++count;
sum+=value;
inFile>>value;
}

if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mimatch.\n";
else
cout<<"Input terminated for unknow reasons.\n";
if(count==0)
cout<<"No data procesed.\n";
else
{
cout<<"Item read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<sum/count<<endl;
}

system("pause");
return 0;


}

我运行的时候可以了,你看看!


java,c++...thinking......
2006-09-27 01:43
wyg_616
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-9-24
收藏
得分:0 

上面的程序我运行了的,是可以的呀.不晓得 你的环境那里出了问题哈


胸有成竹,而面如平湖这可拜上将军
2006-09-27 12:12
快速回复:读入文件的问题
数据加载中...
 
   



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

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