在C中如何使用不同类型的变量编写二进制文件,然后在Python中进行读取?

huangapple go评论68阅读模式
英文:

How can I write binary file in C with different types of variables and read in Python that?

问题

直到现在,我在C中以“csv”格式保存文件。但是,文件的大小很大,所以I/O步骤的执行时间对我来说非常长。因此,我尝试在C中保存二进制文件,但在Python中读取文件时会插入一些垃圾数据。

文件中的数据类型如下:

int double int double double ...

我在C中尝试了以下内容:

...

FILE* fp_result = fopen([filepath]); // 文件格式是 .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);

然后我在Python中尝试了以下内容:

import numpy as np

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

结果是

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

如何解决这个问题?

英文:

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?

答案1

得分: 1

这段代码:

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

会立即崩溃。你需要:

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

你还需要启用最大编译警告 (-Wall -Wextra 如果使用GCC),这样编译器会告诉你上面的错误。

修复这个问题后,你只需在Python中处理二进制数据。请参考这个答案

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 10:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定