有没有更好的方法在 C 中读取复杂的二进制数据?

发布于 2024-10-16 07:03:31 字数 1482 浏览 5 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2024-10-23 07:03:31

直接声明zbuff,不需要中间缓冲区。为此,您需要在适当的位置进行以下更改。在 fread 中,不是读取大小 1,而是读取 sizeof(dcomplex)。这应该可以做到。

    //buffer = (char*)malloc(sizeof(char)*size);
    zbuff = (dcomplex*)malloc(sizeof(dcomplex)*size);
    if(zbuff == NULL)
    {
        fputs("Buffer allocation failed.", stderr);
        exit(1);
    }

    result = fread(zbuff, sizeof(dcomplex), size, input);
    if(result != size)
    {
        fputs("File reading failed.", stderr);
        exit(2);
    }

    /* The cast I'm referring to */
    //zbuff = (dcomplex*)buffer;

    .......

    free(zbuff);

将所有出现的“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.

    //buffer = (char*)malloc(sizeof(char)*size);
    zbuff = (dcomplex*)malloc(sizeof(dcomplex)*size);
    if(zbuff == NULL)
    {
        fputs("Buffer allocation failed.", stderr);
        exit(1);
    }

    result = fread(zbuff, sizeof(dcomplex), size, input);
    if(result != size)
    {
        fputs("File reading failed.", stderr);
        exit(2);
    }

    /* The cast I'm referring to */
    //zbuff = (dcomplex*)buffer;

    .......

    free(zbuff);

Replace all occurrences of 'buffer' with 'zbuff'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文