matlab与c/cpp之间的fwrite和fread
fwrite and fread between matlab and c/cpp
当我使用fwrite在MATLAB中保存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);
}
Windows,对吗?默认情况下,文件以文本模式打开,字节26被解释为EOF标记。将fopen
重写为fopen("r01.bin", "rb")
,以强制以二进制模式打开文件。
您确定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 );
相关文章:
- 对fread的返回值感到困惑
- 将fwrite转换为C++类型代码以写入二进制文件
- 在数据写入磁盘之前,fwrite 会阻塞吗?
- 这个 matlab 代码的C++等价物是什么(fread matlab vs fread C/C++)?
- 我如何使用ifstream使用以下类似fread()的语句?
- 服务器状态到 XML 使用 fwrite?
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- fwrite 4 字符数组,将写入 7 而不是 4
- 奇怪的内存消耗 C fread / C++ 读取函数,基于 Linux sysinfo 数据
- 在C++中使用 fread() 来解释这个结果是什么?
- 我应该如何看待 fread() 的参数?
- 如何正确地 fread() 和 fwrite() 一个非文本文件?
- 使用 fwrite() 和 fread() 进行二进制输入/输出
- 使用 fwrite/fread 对矩阵进行二进制(反)序列化不起作用
- 为什么要使用fopen打开流,并使用读/写(而不是fread/fwrite)执行I/O
- 如果要显式指定缓冲区大小,应使用 Fread/FWRITE 还是读/写系统调用
- 我正在尝试使用 fread 和 fwrite 来制作复制函数,但根据缓冲区大小,我不断得到奇怪的输出
- 使用fread/fwrite将文件复制到USB的性能
- "fwrite"在"fread"之后不起作用?
- matlab与c/cpp之间的fwrite和fread