C 在读取 .ppm 时不转到下一行

C does not go to the next line while reading .ppm

所以我有一个 .ppm 文件,目标是将每个像素读取到相应的 r[] g[] 和 b[] 元素。代码读取了第一行(idk 正确与否),但它不再继续。我不确定我是否需要这些 getc(fp); 来跳过空格。读取每一行并将其解析为 int 不是一种选择。感谢您的帮助。

int main(int argc, char** argv) 
    {
        int height;
        int width;
        int max;
        FILE *fp;
        fp = fopen("vit_small.ppm", "r");
        fscanf(fp, "%*[^\n]\n", NULL);
        fscanf(fp, "%d %d", &width, &height);
        printf("Width is %d height is %d \n", width, height);
        fscanf(fp, "%d", &max);
        printf("Maximum value %d \n", max);
        int r [height][width];
        int g [height][width];
        int b [height][width];
        int hist [5];
        int w = 0;
        int h = 0;
        char buffer [1000];
        for (;w<height;w++)
        {
            printf("Row number %d \n", w);
            for (;h<width;h++)
                {
                fread(&r[w][h], 1, 1, fp);
                printf("%d ", r[w][h]);
                getc(fp);
                fread(&g[w][h], 1, 1, fp);
                printf("%d ", g[w][h]);
                getc(fp);
                fread(&b[w][h], 1, 1, fp);
                printf("%d ", b[w][h]);
                getc(fp);
                }
            getc(fp);
            printf("\n");
        }

        int i = 0;
        int j = 0;
        for (;i<height; i++) 
        {
            for (;j<width; j++) 
            {
                printf("%d %d %d ", r[i][j], g[i][j], b[i][j]);
            }
        printf("\n");
        }

     fclose(fp);
     FILE * res;
     res = fopen ("Image_output.ppm", "w");
     fprintf (res, "P6\n");
     fprintf(res, "%d\n", width);
     fprintf(res, "%d\n", height);
     fprintf(res, "%d\n", max);
     i = 0;
     j = 0;
     for(; i < height; i++)
     {
         for(; j < width; j++)
         {
             fprintf(res, "%d %d %d", r[i][j], g[i][j], b[i][j]);
         }
         fprintf(res,"\n");
     }
        return (EXIT_SUCCESS);
    }

PPM的P6格式将每个primary存储为一个字节,没有行也没有空格。因此,如果图像是 10 x 6,则在 255 和换行符之后将有 180 个字节 (10x6x3)。看 Wikipedia entry on PPM.