C++ matlab与c/cpp之间的fwrite和fread

C++ matlab与c/cpp之间的fwrite和fread,c++,c,matlab,C++,C,Matlab,当我在MATLAB中使用fwrite保存460个元素的单个变量时,当我尝试在MATLAB中读取其精细值,但尝试在Visual C中使用fread访问相同的bin文件时,对于前88个左右的值给出了精细的结果,但随后它遇到了EOF左右,例如对于其余的元素,它没有给出所需的结果。用于Visual C的代码如下所示 虽然这个问题在过去的一些论坛上也被问过,但答案并不能解决问题 void main() { FILE *p; long lsize; float *temp; int i; size_t n

当我在MATLAB中使用fwrite保存460个元素的单个变量时,当我尝试在MATLAB中读取其精细值,但尝试在Visual C中使用fread访问相同的bin文件时,对于前88个左右的值给出了精细的结果,但随后它遇到了EOF左右,例如对于其余的元素,它没有给出所需的结果。用于Visual C的代码如下所示

虽然这个问题在过去的一些论坛上也被问过,但答案并不能解决问题

void main() 
{
FILE *p;
long lsize;
float *temp;
int i;
size_t nn;
// Name of file
printf("Open File: r0.bin ");
p = fopen("r01.bin", "r");
// Determine the size of file
fseek (p, 0 , SEEK_END);
lsize = ftell (p);
rewind (p);
// Allocate memory
int a=sizeof(float);
lsize /= a;
temp = (float*) malloc (a*lsize);
   // Reading the file
nn= fread(temp,a,lsize,p);
// printing the results
for (i=0;i<lsize;i+=4)
  printf("\n %g %g %g %g",temp[i],temp[i+1],temp[i+2],temp[i+3] );
getch();
fclose(p);
} 
void main()
{
文件*p;
长时间lsize;
浮动*温度;
int i;
尺寸nn;
//文件名
printf(“打开文件:r0.bin”);
p=fopen(“r01.bin”、“r”);
//确定文件的大小
fseek(p,0,SEEK_END);
lsize=ftell(p);
倒带(p);
//分配内存
int a=sizeof(浮动);
lsize/=a;
temp=(浮点*)malloc(a*lsize);
//读取文件
nn=fread(温度,a,lsize,p);
//打印结果

对于(i=0;i您确定MATLAB输出的是浮点数而不是双精度数吗?这段代码有点不必要:

// get rid of these 2 statements
// int a=sizeof(float);
// lsize /= a;

temp = (float*) malloc( lsize );

// Reading the file
nn = fread( temp, 1, lsize, p );

Windows,对吗?文件默认以文本模式打开,字节26被解释为EOF标记。将
fopen
重写为
fopen(“r01.bin”,“rb”)
以二进制模式强制打开文件。

你能发布r01.bin吗?我不确定格式…你能发布你用来写入数据的MATLAB代码吗?fi=fopen('r01.bin','w');count=fwrite(fi,r0,'single');fclose(fi);其中r0是一个由1x469个单个元素组成的数组。我得到了它,主要的问题不是用'r'读取它,而是用'rb'作为二进制文件…p=fopen(“r01.bin”,“rb”);这是正确的,但是@snabbasi在三小时前的评论中发布了这个解决方案。