chap06/helix.c

サンプルコードのダウンロード

 1/* -*- C -*- */
 2#include <stdio.h>
 3
 4int main()
 5{
 6  const int N = 32;
 7  const char fn[] = "helix2.dat";
 8
 9  int i, header, footer;
10  double x[N], y[N], z[N];
11  FILE *fp;
12
13  fp = fopen(fn, "r");
14
15  if( fp == NULL ) {
16    printf("Failed to open file\n");
17    return -1;
18  }
19
20  //
21  // *Most* fortran compilers generate a code such that each fortran `write'
22  // statement add header and footer before and after the actual data. Usually
23  // the header and footer are 4 byte integers that describe the size of
24  // data. However, this is not a fortran standard and the behavior may differ
25  // in general.
26  //
27
28  // -*- read x -*-
29
30  // read 4 byte integer as header
31  fread(&header, sizeof(4), 1, fp);
32
33  // read N double precision data
34  fread(x, sizeof(double), N, fp);
35
36  // read 4 byte integer as footer
37  fread(&footer, sizeof(4), 1, fp);
38
39  // same thing for y
40  fread(&header, sizeof(4), 1, fp);
41  fread(y, sizeof(double), N, fp);
42  fread(&footer, sizeof(4), 1, fp);
43
44  // same thing for z
45  fread(&header, sizeof(4), 1, fp);
46  fread(z, sizeof(double), N, fp);
47  fread(&footer, sizeof(4), 1, fp);
48
49  fclose(fp);
50
51  // output
52  for(int i=0; i < N ;i++) {
53    printf("%5.2f %5.2f %5.2f\n", x[i], y[i], z[i]);
54  }
55
56  return 0;
57}