英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论