简体   繁体   English

如何在C ++中读取形状未知的二进制文件?

[英]How do I read a binary file of unknown shape in C++?

I have a binary file with a 3600 byte header, 13483 traces (columns) each with a 240 byte header. 我有一个带3600字节标头的二进制文件,每条都有240字节标头的13483条迹线(列)。 I want to skip the headers and read the data values into a matrix. 我想跳过标题并将数据值读入矩阵。

I'm able to get some values out of the file but the seismicDataNH[50][40] to seismicDataNH[50][54] should be [13, 17, 12, 5, 19, 51, 29, -118, -127, -127, -50, 126, 126, 126, -32] which is not what I get. 我可以从文件中获取一些值,但地震数据NH [50] [40]至地震数据NH [50] [54]应该为[13,17,12,5,5,19,51,29,-118,- 127,-127,-50,126,126,126,-32],这不是我得到的。

I'm not sure I understand fread() correctly, does it read the file as one long row of values, or as multiple line? 我不确定我是否正确理解fread(),它会将文件读取为一长列值还是多行? I'm assuming one long row, maybe that's why it doesn't work. 我假设一行很长,也许这就是为什么它行不通的原因。

Here's the code I wrote to read the file: 这是我编写的读取文件的代码:

#include <iostream>

using namespace std;

#define N_SAMP 1990
#define M_TR 13483

char tempArray [N_SAMP*M_TR];
char seismicData[1990][13483];
char seismicDataNH[1750][13483];

int main()
{
    FILE*seismicFile;
    seismicFile = fopen("NVGT-88-06.sgy","rb");

    if (seismicFile!=NULL)
    {
        fseek(seismicFile, 3600*sizeof(char), SEEK_CUR);
        fread(tempArray, sizeof(char), N_SAMP*M_TR, seismicFile);
        puts("\n\nRead File successfully");

        int c = 0;
        for (int in=0; in<N_SAMP; in++)
        {
            for (int im=0; im<M_TR; im++)
            {
                seismicData[in][im] = tempArray[c];
                c++;
            }
        }

        puts("\nStored in matrix");


        // Make matrix values without header values
        for (int in=240; in < N_SAMP; in++)
        {
            for(int im=0; im < M_TR; im++)
            {
                seismicDataNH[in-240][im] = seismicData[in][im];
            }
        }
        puts("Removed header");



        puts("Test values: \n");
        for (int it = 40; it<55; it++)
        {
            printf("%d\n", seismicDataNH[50][it]);
        }

        fclose(seismicFile);

    }
    return 0;
}

and here's the data file (.sgy) if someone wants to have a look at it: https://www.dropbox.com/s/y8aa99yqhfyacc8/NVGT-88-06.sgy?dl=0 这是数据文件(.sgy),如果有人想看一下: https ://www.dropbox.com/s/y8aa99yqhfyacc8/NVGT-88-06.sgy?dl =0

From your description, there are 13483 consecutive blocks of 1990 bytes (including 240 for the header). 根据您的描述,有1990个字节的13483个连续块(包括240个标头)。

That means you have the for loop nesting and array indexing the wrong way around. 这意味着您使用了错误的方式进行for循环嵌套和数组索引。

Change the array definitions to : 将数组定义更改为:

char seismicData[M_TR][N_SAMP];
char seismicDataNH[M_TR][N_SAMP-240];

And the two nested for loops to : 而两个嵌套的for循环到:

for (int im=0; im<M_TR; im++)
{
    for (int in=0; in<N_SAMP; in++)
    {
        seismicData[im][in] = tempArray[c];
        c++;
    }
}

resp. 分别 :

for(int im=0; im < M_TR; im++)
{
    for (int in=240; in < N_SAMP; in++)
    {
        seismicDataNH[im][in-240] = seismicData[im][in];
    }
}

Keep the final for loop (that prints the data) as it is. 保持最终的for循环(打印数据)不变。

That should give you the expected output (it does for me). 那应该给您预期的输出(对我有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM