有没有更好的方法在 C 中读取复杂的二进制数据?
我用 C 编写了一些代码来读取包含复数的二进制文件。它有效,但我对我需要表演的演员阵容感到不舒服。有更好的办法吗?速度在我的程序中至关重要(我通过将代码从 C++ iostream 更改为 C stdio 函数,使执行时间加倍)。还可以做得更快吗?
这是我的代码:
#include<complex.h>
#include<errno.h>
#define spaceh 6912
#define Nc 3
#define dirac 4
... ...
typedef double complex dcomplex;
long size;
size_t result;
char filename[84];
char* buffer;
dcomplex* zbuff;
int i, j, k, srccol, srcdir;
srcdir = 1;
srccol = 2;
/* allocate array dcomplex QM[dirac][Nc][space] on the heap */
sprintf(filename, "/<path>/file.%d.%d.bin", srcdir, srccol);
FILE* input;
input = fopen(filename, "rb");
if(readfile)
{
fseek(input, 0, SEEK_END);
size = ftell(input);
rewind(input);
buffer = (char*)malloc(sizeof(char)*size);
if(buffer == NULL)
{
fputs("Buffer allocation failed.", stderr);
exit(1);
}
result = fread(buffer, 1, size, input);
if(result != size)
{
fputs("File reading failed.", stderr);
exit(2);
}
/* The cast I'm referring to */
zbuff = (dcomplex*)buffer;
}
else
{
printf("File was not successfully opened: %s\n", strerror(errno));
}
count = 0;
for(k = 0; k < space; k++)
{
for(j = 0; j < Nc; j++)
{
for(i = 0; i < dirac; i++)
{
QM[i][j][k] = convert_complex(zbuff(count));
count++;
}
}
}
free(buffer);
fclose(input);
convert_complex 函数反转单个复数的字节顺序。我对此感到更不舒服,但我不想让我的问题变得太大。
I have written some code in C to read a binary file containing complex numbers. It works, but I am uncomfortable with the cast I need to perform. Is there a better way? Speed is critical in my program (I doubled the execution time by changing the code from C++ iostreams to C stdio functions). Can it also be made faster?
Here is my code:
#include<complex.h>
#include<errno.h>
#define spaceh 6912
#define Nc 3
#define dirac 4
... ...
typedef double complex dcomplex;
long size;
size_t result;
char filename[84];
char* buffer;
dcomplex* zbuff;
int i, j, k, srccol, srcdir;
srcdir = 1;
srccol = 2;
/* allocate array dcomplex QM[dirac][Nc][space] on the heap */
sprintf(filename, "/<path>/file.%d.%d.bin", srcdir, srccol);
FILE* input;
input = fopen(filename, "rb");
if(readfile)
{
fseek(input, 0, SEEK_END);
size = ftell(input);
rewind(input);
buffer = (char*)malloc(sizeof(char)*size);
if(buffer == NULL)
{
fputs("Buffer allocation failed.", stderr);
exit(1);
}
result = fread(buffer, 1, size, input);
if(result != size)
{
fputs("File reading failed.", stderr);
exit(2);
}
/* The cast I'm referring to */
zbuff = (dcomplex*)buffer;
}
else
{
printf("File was not successfully opened: %s\n", strerror(errno));
}
count = 0;
for(k = 0; k < space; k++)
{
for(j = 0; j < Nc; j++)
{
for(i = 0; i < dirac; i++)
{
QM[i][j][k] = convert_complex(zbuff(count));
count++;
}
}
}
free(buffer);
fclose(input);
The convert_complex function reverses the byte order of a single complex number. I'm even more uncomfortable with that, but I don't want my question to become too large.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接声明zbuff,不需要中间缓冲区。为此,您需要在适当的位置进行以下更改。在 fread 中,不是读取大小 1,而是读取 sizeof(dcomplex)。这应该可以做到。
将所有出现的“buffer”替换为“zbuff”。
Declare the zbuff directly, without the need for intermediate buffer. For that you will need the following changes at appropriate places. In fread, instead of reading size 1, read sizeof(dcomplex). This should do it.
Replace all occurrences of 'buffer' with 'zbuff'.