简体   繁体   English

Ansi C从PPM文件中读取freez

[英]Ansi C read from PPM file freez

I am struggling with some issue in my ansi c code. 我在自己的ansi c代码中遇到了一些问题。 I want to read PPM picture (P6) and then write it to the color (int) arrays. 我想读取PPM图片(P6),然后将其写入颜色(int)数组。 Everything should work great, but unfortunatelly it isn't. 一切都应该很好,但不幸的是,事实并非如此。

The problem is, that the reading freezes at some point - at this same point each time. 问题是,读数会在某个时间点冻结​​-每次都在同一时间点冻结​​。

This is a code for reading: 这是用于阅读的代码:

int **red,**blue,**green;
    //... Some code, allocations etc.
    static unsigned char a[3];
    for(i = 0; i < x; i++)
    {
           for(j = 0; j < y; j++)
           {
                fread(a, 1, 3, pic);
                red[i][j] = a[0];
                //green[i][j] = a[1];
                //blue[i][j] = a[2];
           }
    }

When it reads value 24 it is getting crazy - before everything is ok. 当它读取值24时,它会变得疯狂-在一切正常之前。 This value is asigned to the red and it is 24 till the end of loops. 该值分配给红色,直到循环结束为止为24。

I have noo idea, what is going on there, so this is why I am asking you guys, for some advice - what can be wrong here and how it can be fixed? 我不知道,这是怎么回事,所以这就是为什么我要向你们征求一些建议-这里可能出什么问题以及如何解决?

EDIT 编辑

This is my allocation: 这是我的分配:

red = (int **)malloc(sizeof(int*) * x);
green = (int **)malloc(sizeof(int*) * x);
blue = (int **)malloc(sizeof(int*) * x);

for (i = 0; i < x; i++)
{
       red[i] = (int*)malloc(sizeof(int) * y);
       green[i] = (int*)malloc(sizeof(int) * y);
       blue[i] = (int*)malloc(sizeof(int) * y);
}

EDIT2: 编辑2:

X is 473 an Y is 600 X是473而Y是600

EDIT3: 编辑3:

I did as told and uploaded picture (different this time) with my source code on dropbox: code . 我按照提示进行了操作,并在dropbox: code上上传了源代码(这次是不同的)。 Despite different picture still it freez at some point. 尽管图片不尽相同,但仍会在某些时候冻结。 This time it is 70. 这次是70。

In the code you linked to, you're not opening the file in binary mode : "rb" . 在链接到的代码中,您没有以二进制模式"rb"打开文件。

See : what's the differences between r and rb in fopen 请参阅: fopen中的r和rb有什么区别

Note that fixing this might mean your fscanf s need tweaking. 请注意,修复此问题可能意味着您的fscanf需要进行调整。

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

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