可以使用fstream准确读取文件中的所有数据 [英] Can read all data off file accurately with fstream

查看:1092
本文介绍了可以使用fstream准确读取文件中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来读取文本

文件中的2,000,000个浮点数,以计算总和,平均值和中位数。这是

Stroustrup的论文的直接例子。


但程序不会显示元素总数beyone 23,272:


#include< vector>

#include< fstream>

#include< iostream>

#include<算法>

使用命名空间std;


int main(int argc,char * argv [])

{

char * file = argv [2];

vector< double> buf;


double median = 0;

double mean = 0;


fstream fin(" num .txt",ios :: in); //打开文件输入

双d;


while(fin>> d){

buf.push_back (d);

mean =(buf.size()== 1)? d:mean +(d-mean)/buf.size(); //倾向于

四舍五入错误

}


sort(buf.begin(),buf.end());


if(buf.size()){

int mid = buf.size()/ 2;

median = (buf.size()%2)? buf [mid] :( buf [mid-1] + buf [mid])/ 2;

}


cout<< 元素数量= << buf.size()

<< ",median =" <<中值<< ",mean =" <<平均<< \ n;


}


任何人都有任何想法吗?


- Don Kim

I''m writing a program to read 2,000,000 floating point numbers off a text
file, to compute the sum, mean, and median. This is a direct example of
Stroustrup''s paper.

But the program will not display the total number of elements beyone 23,272:

#include<vector>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;

int main(int argc, char* argv[])
{
char* file = argv[2] ;
vector<double> buf;

double median = 0;
double mean = 0;

fstream fin("num.txt", ios::in) ; // open file for input
double d;

while(fin >> d) {
buf.push_back(d) ;
mean = (buf.size()==1) ? d : mean+(d-mean)/buf.size(); // prone to
rounding errors
}

sort(buf.begin() ,buf.end()) ;

if (buf.size()) {
int mid = buf.size()/2;
median = (buf.size()%2) ? buf[mid] : (buf[mid-1]+buf[mid])/2;
}

cout << "Number of elements = " << buf.size()
<< ", median = " << median << ", mean = " << mean << "\n";

}

Anyone have any ideas?

-Don Kim

推荐答案

Don Kim写道:
Don Kim wrote:

任何人都有任何想法?

Anyone have any ideas?




我在近2000万个值上未修改你的代码。


元素数量= 19315296,中位数= 3.4,平均值= 3.03333


你确定文件中有200万个元素吗?



I ran your code unmodified on almost 20 million values.

Number of elements = 19315296, median = 3.4, mean = 3.03333

Are you sure you have 2 million elements in your file ?


我在windows xp上运行了2000000个数字的文件,编译程序

与gcc - 没问题...

I ran also with 2000000 numbers in file on windows xp, compiled program
with gcc - no problems...


" Gianni Mariani" < GI ******* @ mariani.ws>写了
"Gianni Mariani" <gi*******@mariani.ws> wrote
你确定文件中有200万个元素吗?
Are you sure you have 2 million elements in your file ?




是的。


我编写了一个程序来为文本文件生成随机数。最初,我

让它产生这样的数字:


98.989

72.585

58.986


当数字是浮点数并且格式化时,我得到奇怪的

行为。但是当我让程序生成这样的整数数字时

代替:


98

75

58

程序运行正常。


我在Windows XP上运行它,并在VC 7.1,8.0上测试它, gcc 3.3.3和

Digital Mars 8.4.1并在所有编译器上获得相同的奇怪行为。


困惑为何会出现这种情况。


-Don Kim



Yes.

I wrote a program to generate random numbers to a text file. Originally, I
had it generate the numbers to file like this:

98.989
72.585
58.986

When the numbers are floating point and formatted like that, I get the odd
behavior. But when I make the program generate integer numbers like this
instead:

98
75
58

The program runs correctly.

I''m running this on Windows XP, and tested it on VC 7.1, 8.0, gcc 3.3.3 and
Digital Mars 8.4.1 and get the same odd behavior on all compilers.

Puzzled as to why this is the case.

-Don Kim


这篇关于可以使用fstream准确读取文件中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆