读取1k * 1k数据 [英] read 1k * 1k data

查看:158
本文介绍了读取1k * 1k数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了解决100k * 100k数据,我终于采用了文件读取方法并使用了

malloc。系统以某种方式知道使用虚拟内存。


现在我首先测试1k * 1k数据但是再次发生无法解释的事情。


数据文件开始于:


42.106983132 85.931514337 98.155213938 23.685776453 76.827067592

....

当我打印出阵列时
存储文件内容:


0.000000 k:1020 r:0.000000 k:1021 r:0.000000 k:1022 r:0.000000 k:1023

r:0.000000 k :1024 r:42.106983 k:1025 r:85.931514 k:


可以看到第一行什么都没读(k为索引,r为数组

内容)


我使用函数fscanf来读取文件:


#define SIZE 1024


double * r;

int i = 0;


r =(double *)malloc(SIZE * SIZE * sizeof(double));

while(!feof(file)&& i< SIZE * SIZE){

fscanf(file,"%lf",& r [i]);

i ++;


// if(i< 30)返回;

}

for(k = 0; k< SIZE * SIZE; k ++){

// if(kSIZE * SIZE - 10)

if(k <2048)

printf(" k:%dr:%lf",k,r [k]);

}

实际上如果(i <30)返回没有停止文件读取循环,我需要

来添加i< SIZE * SIZE停止无限阅读。有人知道发生了什么吗?


此外,当我使用scanf,fscanf等在Google搜索时,它会返回

不是那么有用的示例结果。我也非常感谢您在网上搜索帮助的建议

。非常感谢!!

To solve the 100k * 100k data, I finally adopt the file read method and use
malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453 76.827067592
....

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k: 1023
r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {
fscanf(file, "%lf", &r[i]);
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);
}
and indeed if (i < 30) return does not stop the file reading loop and i need
to add i < SIZE * SIZE to stop infinite reading. Anybody knows what''s
happening?

Furthermore, when I use scanf, fscanf etc to search in Google, it returns
not so helpful example results. I''d also appreciate your suggestions on
searching help on web. Thanks a lot!!

推荐答案

a写道:
a wrote:

解决100k * 100k数据,我终于采用了文件读取方法并使用了
malloc。系统以某种方式知道使用虚拟内存。


现在我首先测试1k * 1k数据但是再次发生无法解释的事情。


数据文件开始于:


42.106983132 85.931514337 98.155213938 23.685776453 76.827067592

...


当我打印出数组存储文件内容:


0.000000 k:1020 r:0.000000 k:1021 r:0.000000 k:1022 r:0.000000 k:1023

r:0.000000 k: 1024 r:42.106983 k:1025 r:85.931514 k:


可以看到第一行什么都没读(k为索引,r为数组

内容)


我使用函数fscanf来读取文件:


#define SIZE 1024

double * r;

int i = 0;


r =(double *)malloc(SIZE * SIZE * sizeof(double));


while(!feof(file)&& i< SIZE * SIZE){
To solve the 100k * 100k data, I finally adopt the file read method and use
malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453 76.827067592
...

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k: 1023
r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {



这个错误是问题12.2的主题在

comp.lang.c常见问题(FAQ)列表,

< http://www.c-faq.com/>。

This mistake is the subject of Question 12.2 in the
comp.lang.c Frequently Asked Questions (FAQ) list,
<http://www.c-faq.com/>.


fscanf(file,"%lf",& r [i]);
fscanf(file, "%lf", &r[i]);



你有理由相信这有效吗?

问题12.19没有直接解决这个问题,但是

有一些提示。

Do you have any reason to believe that this worked?
Question 12.19 doesn''t address this issue directly, but
has a few hints about it.


i ++;


// if(i< 30 )返回;

}

for(k = 0; k< SIZE * SIZE; k ++){

// if(kSIZE * SIZE) - 10)

if(k <2048)

printf(" k:%dr:%lf",k,r [k]);
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);



这个错误是问题12.9的主题。


-

Eric Sosman
es*****@ieee-dot-org.inva 盖子




" a" < a@a.com写了留言

"a" <a@a.comwrote in message

为了解决100k * 100k数据,我终于采用了文件读取方法和

使用malloc的。系统以某种方式知道使用虚拟内存。


现在我首先测试1k * 1k数据但是再次发生无法解释的事情。


数据文件开头:


42.106983132 85.931514337 98.155213938 23.685776453

76.827067592 ...


当我打印出阵列存储时文件内容:


0.000000 k:1020 r:0.000000 k:1021 r:0.000000 k:1022 r:0.000000 k:

1023 r:0.000000 k :1024 r:42.106983 k:1025 r:85.931514 k:


可以看到第一行什么都没读(k为索引,r为数组

内容)


我使用函数fscanf来读取文件:


#define SIZE 1024


double * r;

int i = 0;


r =(double *)malloc(SIZE * SIZE * sizeof(double));


while(!feof(file)&& i< SIZE * SIZE){

fscanf(file,"%lf",& r [i ]);

i ++;


// if(i< 30)返回;

}

for(k = 0; k< SIZE * SIZE; k ++){

// if(kSIZE * SIZE - 10)

if(k <2048)

printf(" k:%dr:%lf",k,r [k]);

}


确实如果(i <30)返回没有停止文件读取循环而且我需要添加i
< SIZE * SIZE停止无限阅读。有人知道发生了什么吗?


此外,当我使用scanf,fscanf等在Google搜索时,它会返回

不是那么有用的示例结果。我也非常感谢您在网上搜索帮助的建议

。非常感谢!!
To solve the 100k * 100k data, I finally adopt the file read method and
use malloc. The system somehow knows to use virtual memory.

Now I first test 1k * 1k data but something uninterpretable happens again.

The data file starts with:

42.106983132 85.931514337 98.155213938 23.685776453
76.827067592 ...

when I print out the array storing the file content:

0.000000 k: 1020 r: 0.000000 k: 1021 r: 0.000000 k: 1022 r: 0.000000 k:
1023 r: 0.000000 k: 1024 r: 42.106983 k: 1025 r: 85.931514 k:

one can see the first line reading nothing (k as index and r as array
content)

I use the function fscanf to read the file:

#define SIZE 1024

double *r;
int i = 0;

r = (double *)malloc(SIZE * SIZE * sizeof(double));
while(!feof(file) && i<SIZE * SIZE) {
fscanf(file, "%lf", &r[i]);
i++;

// if (i < 30 ) return;
}
for (k=0; k<SIZE*SIZE; k++) {
// if (kSIZE * SIZE - 10 )
if (k< 2048 )
printf("k: %d r: %lf ",k, r[k]);
}
and indeed if (i < 30) return does not stop the file reading loop and i
need to add i < SIZE * SIZE to stop infinite reading. Anybody knows what''s
happening?

Furthermore, when I use scanf, fscanf etc to search in Google, it returns
not so helpful example results. I''d also appreciate your suggestions on
searching help on web. Thanks a lot!!



这对我来说很合适。

尝试在malloc()之后进行测试,以确保你真的有

内存。

在诊断printf()的末尾添加换行符。

然后

1)发布打开文件的代码

2)尝试将文件字符回显到stdout,以确保文件正确读取
。您将需要使用fgetc()。

3)尝试读取临时双精度并打印回来以确保sfcanf()

正确转换信息。 />
-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

This looks OK to me.
Try putting in a test after malloc() to make sure you actually have the
memory.
Also put a newline on the end of your diagnostic printf().
Then
1) post the code where you open the file
2) try echoing the file characters back to stdout to make sure the file is
being read correctly. You will need to use fgetc().
3) Try reading to a temporary double and printing back to make sure sfcanf()
is converting the information properly.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


11月26,上午5:24,Ben Pfaff< b ... @ cs.stanford.eduwrote:
On Nov 26, 5:24 am, Ben Pfaff <b...@cs.stanford.eduwrote:

" a" < a ... @ a.comwrites:
"a" <a...@a.comwrites:

#define SIZE 1024
#define SIZE 1024


double * r;

int i = 0;
double *r;
int i = 0;


r =(double *)malloc(SIZE * SIZE * sizeof(double));
r = (double *)malloc(SIZE * SIZE * sizeof(double));



这个大小计算的最可能结果是8388608.你在'int''可以保存这个值的系统上是

吗? `int''可能有
a最大值小到32767.


The most likely result of this size calculation is 8388608. Are
you on a system where `int'' can hold this value? `int'' may have
a maximum value as small as 32767.



sizeof(double)给出一个size_t,所以int是SIZE * SIZE

= 1048576将被提升为size_t进行乘法。

sizeof(double) gives a size_t, so the int that is SIZE*SIZE
= 1048576 will be promoted to size_t for the multiplication.


这篇关于读取1k * 1k数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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