在 C 中读取 python 生成的二进制数组时出现分段错误(核心转储)错误
Segmentation fault(core dumped ) error while reading a python generated binary array in C
我正在尝试加载一个由 numpy 创建的二维数组并读取 C 中的元素,但在 运行 时出现 Segmentation fault(core dumped) 错误.代码由
行组成
#include <stdio.h>
#include <string.h>
int main(){
char *file;
FILE *input;
int N1, N2, ii, jj;
float element;
strcpy(file, "/home/caesar/Desktop/test.bin");
input = fopen(file, "rb");
fread(&N1, sizeof(int), 1, input);
fread(&N2, sizeof(int), 1, input);
float memoryarray[N1][N2];
for(ii= 0; ii<N1; ii++){
for(jj=0; jj<N2; jj++){
fread(&element, sizeof(float), 1, input);
memoryarray[ii][jj]= element;
}
}
printf("%f", memoryarray[2][3]);
fclose(input);
return 0;
}
这是一项任务的开始,我将不得不从 400*400*400 左右的矩阵中读取元素。这个想法是从文件中读取所有元素并将其存储在内存中,然后在必要时从内存索引中读取,例如,这里我试图访问和打印第二行第三列中的元素。
P.S: 我对指针很陌生。
亲爱的,我试过你说的方法。这里是代码的修改版本,分段错误消失了,但输出全为零,或者只是普通的垃圾值。
我 运行 执行了三次可执行文件,我得到的输出是
输出1:-0.000000
输出 2:0.000000
输出 3:-97341413674450944.000000
顺便说一句,我的数组包含整数
这里是修改后的代码
#include <stdio.h>
#include <string.h>
void main(){
const char file[] ="/home/caesar/Desktop/test.bin";
FILE *input;
int N1, N2, ii, jj;
float element;
//strcpy(file, "/home/caesar/Desktop/test.bin");
input = fopen(file, "r");
fread(&N1, sizeof(int), 1, input);
fread(&N2, sizeof(int), 1, input);
float memoryarray[N1][N2];
for(ii= 0; ii<N1; ii++){
for(jj=0; jj<N2; jj++){
fread(&element, sizeof(float), 1, input);
memoryarray[ii][jj]= element;
}
}
printf("%f", memoryarray[1][2]);
fclose(input);
这里还有我试图打开的文件的十六进制转储。你们中的一些人让我验证 fopen() 是否在工作,我检查了,它在工作。
00000000 00 00 40 40 00 00 40 40 01 00 00 00 00 00 00 00 |..@@..@@........|
00000010 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 |................|
*
00000030 04 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 |................|
00000040 05 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00 |................|
00000050
所以这是我的问题的简要说明。我使用 python 将双精度浮点数的多维数组写入文件。我想通过使用元素的索引获取值来获取这些文件并在必要时访问元素。这样做的任何 C 代码都可以解决我的问题。
这是我用来编写文件
的 python 代码
with open('/home/caesar/Desktop/test.bin', 'wb') as myfile:
N= np.zeros(2, dtype= np.float32, order= "C")
N[0]= 3
N[1]= 3
a= [[1,2,3],[2,3,4], [4,5,6]]
N.astype(np.float32).tofile(myfile)
b= np.asarray(a)
b.tofile(myfile)
strcpy(file, "/home/caesar/Desktop/test.bin");
这将写入垃圾内存地址。
您应该将文件声明为合适大小的数组,如下所示:
char file[100];
或
直接用这样的路径初始化char指针(去掉strcpy):
const char *file = "/home/caesar/Desktop/test.bin";
或最佳,根据共识(参考评论):
fopen("/home/caesar/Desktop/test.bin", "rb");
我正在尝试加载一个由 numpy 创建的二维数组并读取 C 中的元素,但在 运行 时出现 Segmentation fault(core dumped) 错误.代码由
行组成
#include <stdio.h>
#include <string.h>
int main(){
char *file;
FILE *input;
int N1, N2, ii, jj;
float element;
strcpy(file, "/home/caesar/Desktop/test.bin");
input = fopen(file, "rb");
fread(&N1, sizeof(int), 1, input);
fread(&N2, sizeof(int), 1, input);
float memoryarray[N1][N2];
for(ii= 0; ii<N1; ii++){
for(jj=0; jj<N2; jj++){
fread(&element, sizeof(float), 1, input);
memoryarray[ii][jj]= element;
}
}
printf("%f", memoryarray[2][3]);
fclose(input);
return 0;
}
这是一项任务的开始,我将不得不从 400*400*400 左右的矩阵中读取元素。这个想法是从文件中读取所有元素并将其存储在内存中,然后在必要时从内存索引中读取,例如,这里我试图访问和打印第二行第三列中的元素。
P.S: 我对指针很陌生。
亲爱的,我试过你说的方法。这里是代码的修改版本,分段错误消失了,但输出全为零,或者只是普通的垃圾值。 我 运行 执行了三次可执行文件,我得到的输出是 输出1:-0.000000 输出 2:0.000000 输出 3:-97341413674450944.000000
顺便说一句,我的数组包含整数
这里是修改后的代码
#include <stdio.h>
#include <string.h>
void main(){
const char file[] ="/home/caesar/Desktop/test.bin";
FILE *input;
int N1, N2, ii, jj;
float element;
//strcpy(file, "/home/caesar/Desktop/test.bin");
input = fopen(file, "r");
fread(&N1, sizeof(int), 1, input);
fread(&N2, sizeof(int), 1, input);
float memoryarray[N1][N2];
for(ii= 0; ii<N1; ii++){
for(jj=0; jj<N2; jj++){
fread(&element, sizeof(float), 1, input);
memoryarray[ii][jj]= element;
}
}
printf("%f", memoryarray[1][2]);
fclose(input);
这里还有我试图打开的文件的十六进制转储。你们中的一些人让我验证 fopen() 是否在工作,我检查了,它在工作。
00000000 00 00 40 40 00 00 40 40 01 00 00 00 00 00 00 00 |..@@..@@........|
00000010 02 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 |................|
*
00000030 04 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 |................|
00000040 05 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00 |................|
00000050
所以这是我的问题的简要说明。我使用 python 将双精度浮点数的多维数组写入文件。我想通过使用元素的索引获取值来获取这些文件并在必要时访问元素。这样做的任何 C 代码都可以解决我的问题。
这是我用来编写文件
的 python 代码with open('/home/caesar/Desktop/test.bin', 'wb') as myfile:
N= np.zeros(2, dtype= np.float32, order= "C")
N[0]= 3
N[1]= 3
a= [[1,2,3],[2,3,4], [4,5,6]]
N.astype(np.float32).tofile(myfile)
b= np.asarray(a)
b.tofile(myfile)
strcpy(file, "/home/caesar/Desktop/test.bin");
这将写入垃圾内存地址。 您应该将文件声明为合适大小的数组,如下所示:
char file[100];
或
直接用这样的路径初始化char指针(去掉strcpy):
const char *file = "/home/caesar/Desktop/test.bin";
或最佳,根据共识(参考评论):
fopen("/home/caesar/Desktop/test.bin", "rb");