简体   繁体   English

在 c 中读取和写入文件 - 压缩矩阵

[英]read and write to file in c - compressing matrix

I'm trying to write a compressed bits matrix to a and the read it back.我正在尝试将压缩位矩阵写入 a 并将其读回。 The writing works great but the reading keeps failing and I dont understand why.写作很好,但阅读一直失败,我不明白为什么。

    typedef unsigned char BYTE;

    int saveCompressImageToFile(const char *fileName, const int *image, int row, int col)
    {
        FILE *fp = fopen(fileName, "wb");
        if (!fp)
            return 0;
        if (fwrite(&row, sizeof(int), 1, fp) != 1) {
            fclose(fp);
            return 0;
        }
        if (fwrite(&col, sizeof(int), 1, fp) != 1) {
            fclose(fp);
            return 0;
        }
        int byteCount = row * col / 8;
        if (byteCount * 8 < row * col)
            byteCount++;
        BYTE *data = (BYTE *)calloc(sizeof(BYTE), byteCount);
        int dataPos;
        int dataShift;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                dataPos = (i * col + j) / 8;
                dataShift =7 - (i * col + j) % 8;
                data[dataPos] |= (*(image + i * col + j)) << dataShift;
            }
        }
        if (fwrite(data, sizeof(BYTE), byteCount, fp) != byteCount) {
            fclose(fp);
            return 0;
        }
        return 1;
    }
    
    int * getImage_compress(const char * fileName, int * pRow, int * pCol)
{
    FILE* fp = fopen(fileName, "rb");
    if (!fp)
        return NULL;
    if (fread(pRow, sizeof(int), 1, fp) != 1)
    {
        fclose(fp);
        return NULL;
    }
    if (fread(pCol, sizeof(int), 1, fp) != 1)
    {
        fclose(fp);
        return NULL;
    }
    size_t mat_size = *pRow * (*pCol);
    int byteCount = mat_size / 8;
    if (byteCount * 8 < mat_size)
        byteCount++;
    BYTE* data = (BYTE*)malloc(sizeof(BYTE)*byteCount);
    if (!data)
    {
        fclose(fp);
        return NULL;
    }
    if (fread(data, sizeof(BYTE), byteCount, fp) != byteCount)
    {
        free(data);
        fclose(fp);
        return NULL;
    }
    fclose(fp);

    int* mat = (int*)calloc(mat_size, sizeof(int));
    if (!mat)
    {
        free(data);
        fclose(fp);
        return NULL;
    }
    for (int i = 0; i < mat_size; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            mat[i * 8 + j] = (data[i] >> (7 - j)) & 0x1;
        }
    }
    free(data);
    return mat;
}

for some reason the function throws exception in the last line (return mat;) , "Starter.exe has triggered a breakpoint".由于某种原因,该函数在最后一行(return mat;)中引发异常,“Starter.exe 已触发断点”。 occurred , what's going on over there?发生了,那边是怎么回事? I've tried to debug that code and I can't tell why I can't return this value but I can access the cells in the debugger.我试图调试该代码,但我不知道为什么我不能返回这个值,但我可以访问调试器中的单元格。 any Suggestions?有什么建议么?

Putting the comments in an answer:将评论放在答案中:

So much wrong.错太多了。

  1. You need to fclose(fp) when there isn't an error in error in your first function.当您的第一个函数中没有错误错误时,您需要fclose(fp) Otherwise not all of the data will be written.否则不会写入所有数据。 Or maybe nothing will be written.或者也许什么都不会写。
  2. fread(&data, ... would try to read the data over the pointer to the allocated space! It should just be fread(data, ... fread(&data, ... 将尝试通过指向分配空间的指针读取数据!它应该只是fread(data, ...
  3. You are reassembling the bits backwards from how you put them in. Change the reassembly to data[i] >> (7 - j) .您正在从放入它们的方式向后重新组装这些位。将重新组装更改为data[i] >> (7 - j) Or assemble them in the other direction.或者将它们组装在另一个方向。
  4. sizeof(BYTE) is 1. You mean 8. Use 8 for those. sizeof(BYTE)是 1。你的意思是 8。使用8
  5. Your allocation of mat when decompressing can be up to seven int s shorter than what you're writing.解压缩时分配的mat最多可以比您正在编写的内容短 7 个int Either allocate mat large enough, using byteCount * 8 , or only decompress rows * columns int s.使用byteCount * 8分配足够大的mat ,或者仅解压缩 rows * columns int s。

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

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