dポイントプレゼントキャンペーン実施中!

初歩的な質問ですみません。
今、819200行のファイルをfscanfで読み込んで配列に格納しているのですが、実行してみるとBus errorが出力されてしまい、うまく格納できません。どなたか至急教えてもらえませんか?

***************************ソース************************************************************
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define pi 3.1415926535 //円周率
#define FILE_SIZE 819200
#define FILE_NAME "test.dat"


struct Data{
int time;
double voltage;
double velocity;
double pressure;
double trigger;
};


int main()
{
int k,n,N,i=0,j;
struct Data dat[FILE_SIZE];

FILE *fp1;
FILE *fp2;

if ((fp1 = fopen(FILE_NAME,"r")) == NULL) {
printf( "file open error\n" );
exit(EXIT_FAILURE);
}


//データの読み込み
while((fscanf(fp1, "%d %lf %lf %lf %lf",dat[i].time,dat[i].voltage,dat[i].velocity,dat[i].pressure,dat[i].trigger)) !=EOF ){
i++;
}

fclose(fp1);


return 0;
}

*****************************************************************************************

********************test.datファイル**********************************************
-2.64316 2.329595 0.697657 0.001373 -4.861982
-2.64314 2.325628 0.671961 0.001984 -4.744793
-2.64312 2.320745 0.640333 0.001678 -4.659953
-2.64310 2.319829 0.634400 0.002289 -4.707866
-2.64308 2.319219 0.630449 0.002289 -4.699321
-2.64306 2.317082 0.616607 0.002594 -4.532387
.
.
.
.
.

***********************************************************************************

環境はmac osです。
よろしくおねがい致します。

A 回答 (3件)

  #define FILE_SIZE 819200


は、ファイルサイズなのではないですか?
  #define LINE_SIZE 819200/(5*8)
の行数にすれば良いのでは。

test.datの各データは、およそ有効桁数8桁となっています。メモリを考えれば struct 構造体の
  double voltage;
などは
  float voltage;
の単精度にすれば有効に取り込む量が増えます。
http://www.cc.kyoto-su.ac.jp/~yamada/programming …

また、円周率πは
  #define pi 3.1415926535
としなくとも「M_PI」を使えば済むことです。
http://hooktail.org/computer/index.php?C%B8%C0%B …

Mac OSX は断らなければ UNIX です。プログラムが簡単なUNIX的使い方をされてはいかがでしょう。
起動は
  ./a.out<text.dat
です。





/* Mac OSX */
#include <stdio.h>
#define LINE_SIZE 819200/(5*8)

struct item{
int time;
float voltage;
float velocity;
float pressure;
float trigger;
};

int main(void)
{
struct item dat[LINE_SIZE], *p;
int i=0;
float temp;

//データの読み込み
p=&dat[i];
while(scanf("%f %f %f %f %f",
&temp,&p->voltage,&p->velocity,&p->pressure,&p->trigger) !=EOF){
p->time=temp*100000; //整数表示に変換
printf("%d %f %f %f %f¥n", p->time,p->voltage,p->velocity,p->pressure,p->trigger);
p=&dat[++i];
}

return 0;
}




----- 実行結果 -----
-264316 2.329595 0.697657 0.001373 -4.861982
-264314 2.325628 0.671961 0.001984 -4.744793
-264312 2.320745 0.640333 0.001678 -4.659953
-264310 2.319829 0.634400 0.002289 -4.707866
-264308 2.319219 0.630449 0.002289 -4.699321
-264306 2.317082 0.616607 0.002594 -4.532387
    • good
    • 0

fscanf の呼び出しをよく見てください.

    • good
    • 0

データの中身の各行頭は実数のようですが これを整数で読み込んでもいいのでしょうか?



また 819200個の構造体をローカル変数にとるのはちょっと考え物のようです
ローカル変数は通常スタック上に取るようになるので 819200個の36バイトだとすると
26214400バイト = 25600Kバイト = 約25MB
の領域が必要です

多くの場合スタックにはそれほど多くの領域を割り当てられていないと思います
VC++の場合 標準では1MBだったように思います

外部変数として main()の前あたりで定義するか mallocなどで動的に取得するほうがいいように思います
    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!