C / Python > Cでfwrite()した値をPythonで読込む > v0.1:1つの値 / v0.2:3つの値 / v0.3:size_tに対応 / v0.4: double complex型の読込み

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
```

C++のコードで生成する係数をJupyter/matplotlibで画像表示したい。
それにはCのfwrite()結果をPythonで読込むことになりそう。

実装してみた。

## v0.1 (1つの値)

### C (write)

```writeTest.c
#include <stdio.h>
#include <stdlib.h>

int main(void){
	FILE *wfp;
	float pi = 3.141592;

	wfp = fopen("sample.bin","wb");
	fwrite(&pi, sizeof(float), 1, wfp);
	fclose(wfp);
}
```

### Python (read)

参考 http://stackoverflow.com/questions/2146031/what-is-the-equivalent-of-fread-from-matlab-in-python

```readTest.py
import numpy as np
import array

with open("sample.bin") as rfp:
	a = array.array("f") # f: typecode for float
	a.fromfile(rfp, 1)
	print (a)
```

上記のtypecodeは以下を参考にした。
https://docs.python.org/3.1/library/array.html


### 実行

sample.bin生成

```
$ gcc writeTest.c && ./a.out
```

sample.bin読込み

```
$ python readTest.py 
array('f', [3.141592025756836]) 
```

読めた。

## v0.2 (3つの値)

### C (write)

```writeTest.c
#include <stdio.h>
#include <stdlib.h>

int main(void){
	FILE *wfp;
	float pi = 3.141592;
	float napier = 2.718;
	float avogadro1 = 6.022;

	wfp = fopen("sample.bin","wb");
	fwrite(&pi, sizeof(float), 1, wfp);
	fwrite(&napier, sizeof(float), 1, wfp);
	fwrite(&avogadro1, sizeof(float), 1, wfp);
	fclose(wfp);
}
```

### Python (read)

```readTest.py
import numpy as np
import array

with open("sample.bin","rb") as rfp:
	a = array.array("f") # f: typecode for float
	a.fromfile(rfp, 3)
	print (a)
```

### 実行

sample.bin生成

```
$ gcc writeTest.c && ./a.out
```

sample.bin読込み

```
$ python readTest.py 
array('f', [3.141592025756836, 2.7179999351501465, 6.021999835968018])
```

## v0.3 (size_t)対応
(追記 2016/12/19)

size_t型の読込みで躓いていた。
v0.1 @ http://qiita.com/7of9/items/68673ac3239532064c28

### size_t型

```sizeofPrint.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	printf("%ld\n", sizeof(size_t));
	printf("%ld\n", sizeof(int));
}
```

```txt:実行
$ gcc sizeofPrint.c && ./a.out 
8
4
```

こちらの環境では`size_t`は8。


### C (write)

```writeTest.c
#include <stdio.h>
#include <stdlib.h>

int main(void){
	FILE *wfp;
	size_t pi = 314;
	float napier = 2.718;
	float avogadro1 = 6.022;

	wfp = fopen("sample.bin","wb");
	fwrite(&pi, sizeof(size_t), 1, wfp);
	fwrite(&napier, sizeof(float), 1, wfp);
	fwrite(&avogadro1, sizeof(float), 1, wfp);
	fclose(wfp);
}
```

### Python (read)

Python側でsize_t(sizeof()=8)の値を読むには[unsigned long]型だろう。

https://docs.python.org/3.1/library/array.html
によると`L`

```readTest.py
import numpy as np
import array

with open("sample.bin","rb") as rfp:
	szv = array.array("L") # L: typecodef for [unsigned long]
	szv.fromfile(rfp, 1)
	print (szv)
	flv = array.array("f") # f: typecode for float
	flv.fromfile(rfp, 2)
	print (flv)
```

```txt:実行
$ python readTest.py 
array('L', [314L])
array('f', [2.7179999351501465, 6.021999835968018])
```

## v0.4 (double complex読込み)
(追記 2016/12/20)

double complex型を読込むにはどうするか。
double型でとりあえず読んでみた。

### C (write)

アボガドロ数の実際の値とは違うがとりあえず。

```writeTest.c
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>

int main(void){
	FILE *wfp;
	size_t pi = 314;
	float napier = 2.718;
	double complex avogadro = 6.022 + 10.23 * I; 

	wfp = fopen("sample.bin","wb");
	fwrite(&pi, sizeof(size_t), 1, wfp);
	fwrite(&napier, sizeof(float), 1, wfp);
	fwrite(&avogadro, sizeof(double complex), 1, wfp);
	fclose(wfp);
}
```

### Python (read)

```readTest.py
import numpy as np
import array

with open("sample.bin","rb") as rfp:
	szv = array.array("L") # L: typecodef for [unsigned long]
	szv.fromfile(rfp, 1)
	print (szv)
	napier = array.array("f") # f: typecode for float
	napier.fromfile(rfp, 1)
	print (napier)
	avogadro = array.array("d") # d: typecode for double
	avogadro.fromfile(rfp, 2) # real and imaginary part of the complex number
	print (avogadro)
```

```txt:実行
$ gcc writeTest.c && ./a.out
$ python readTest.py 
array('L', [314L])
array('f', [2.7179999351501465])
array('d', [6.022, 10.23])
```

とりあえずdouble complexの実部と虚部の読込みはできた。
あとは２つの値を元にcomplex型にキャストするというような方法だろうか。