1

Until now I save a file with a "csv" format in C. However, the size of files are large, so execution time in I/O step is very long for me. Thus, I tried to save a binary file in C, but, some garbage data are inserted when reading a file in Python.

The types of data in file are same for below:

int double int double double ...

I tried in C

...

FILE* fp_result = fopen([filepath]); // the file format is .bin

double w[60];
double th[60];

...

int n = 0;
double K = 10.0;
int num = 30;

fwrite(&n, sizeof(int), 1, fp_result);
fwrite(&K, sizeof(double), 1, fp_result);
fwrite(&num, sizeof(int), 1, fp_result);
fwrite(w, sizeof(double), 60, fp_result);
fwrite(th, sizeof(double), 60, fp_result);

fclose(fp_result);

And then I tried in Python

import numpy as np

x = np.fromfile([filepath])
print(x)

the result is

array[0, 1e-365, ..., 10e0, ...]

how can I solve this problem?

lksj
  • 49
  • 7
  • 1
    `fromfile` needs to know the `dtype` which can be a structure of different data types. See [Structured Arrays](https://numpy.org/doc/stable/user/basics.rec.html) – Michael Butscher Feb 06 '23 at 03:10

1 Answers1

1

This code:

fwrite(n, sizeof(int), 1, fp_result);
fwrite(K, sizeof(double), 1, fp_result);
fwrite(num, sizeof(int), 1, fp_result);

will promptly crash. You want:

fwrite(&n, sizeof(int), 1, fp_result);
fwrite(&K, sizeof(double), 1, fp_result);
fwrite(&num, sizeof(int), 1, fp_result);

You'll also want to enable maximum compiler warnings (-Wall -Wextra if using GCC), so the compiler tells you about the bug above.

After fixing that, all you'll have to do is thread the binary data in Python. See this answer.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Ah.. I missed the "&" symbol before a variable when I wrote this post. However, in actual code I included "&" symbol. thanks! – lksj Feb 07 '23 at 04:05
  • 1
    @lksj And this is why you cut/paste your _actual_ code, and not some mangled version of it. – Employed Russian Feb 07 '23 at 04:56
  • @EmplyedRussian Yeah.. I forgot it for a moment.. Thank you for reminding me of that. – lksj Feb 08 '23 at 05:40