C / Python> Read fwrite () value in C in Python> v0.1: 1 value / v0.2: 3 values / v0.3: corresponds to size_t / v0.4: Read double complex type

Operating environment


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

I want to display an image of the coefficient generated by C ++ code with Jupyter / matplotlib. To do that, I'm likely to read the C fwrite () result in Python.

I implemented it.

v0.1 (one value)

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)

Reference 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)

The above type code is based on the following. https://docs.python.org/3.1/library/array.html

Run

sample.bin generation

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

read sample.bin

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

I read.

v0.2 (3 values)

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)

Run

sample.bin generation

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

read sample.bin

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

v0.3 (size_t) compatible

(Addition 2016/12/19)

I was stumbling on reading the size_t type. v0.1 @ http://qiita.com/7of9/items/68673ac3239532064c28

size_t type

sizeofPrint.c


#include <stdio.h>
#include <stdlib.h>

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

Run


$ gcc sizeofPrint.c && ./a.out 
8
4

In this environment, size_t is 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)

To read the value of size_t (sizeof () = 8) on the Python side, it would be of type [unsigned long].

https://docs.python.org/3.1/library/array.html According to 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)

Run


$ python readTest.py 
array('L', [314L])
array('f', [2.7179999351501465, 6.021999835968018])

v0.4 (double complex read)

(Addition 2016/12/20)

How to read a double complex type? I read it in double type for the time being.

C (write)

It is different from the actual value of Avogadro's number, but for the time being.

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)

Run


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

For the time being, I was able to read the real and imaginary parts of the double complex. Is it a method like casting to a complex type based on the two values?

Recommended Posts

C / Python> Read fwrite () value in C in Python> v0.1: 1 value / v0.2: 3 values / v0.3: corresponds to size_t / v0.4: Read double complex type
Read json in C # and convert to dictionary type (forced)
What to do when the value type is ambiguous in Python?
Type notes to Python scripts for running PyTorch model in C ++ with libtorch
How to generate permutations in Python and C ++
Type Python scripts to run in QGIS Processing
How to handle datetime type in python sqlite3
How to retrieve the nth largest value in Python
[python] Method to darken RGB value in hexadecimal notation
Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)
Deep nesting in Python makes it hard to read
Convert Webpay Entity type to Dict type (recursively in Python)
[Python] How to output the list values in order
How to read csv containing only integers in Python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.