gpt4 book ai didi

c - 如何从 .pgm 文件读取像素值并将其复制到数组中。 C语言编程

转载 作者:行者123 更新时间:2023-11-30 17:01:31 25 4
gpt4 key购买 nike

我已经被这个问题困扰了一段时间了。我如何将 pgm 文件中的像素值 0-255 复制到数组中?这是我到目前为止所拥有的。我知道我偏离了应有的样子。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, int *argv[])
{


int i = 0, j = 0;
unsigned int str[1000];
FILE *pgmFile;

if (argc != 2)
{
printf ("ERROR, Not enough arguments.");
exit(EXIT_FAILURE);
}

if ((pgmFile = fopen(argv[1], "r")) == NULL)
{
printf ("ERROR, File cannot be opened");
exit(EXIT_FAILURE);
}

do
{
fscanf (pgmFile, "%d", str[i]);
printf ("%d", str[i]);
i++;
}
while (str[i] != EOF);



fclose (pgmFile);

return 0;
}

最佳答案

您应该首先阅读标题(3 行)。

使用标题中的数据,您可以设置 for 循环读取 Y 行数。在每一行中,您应该读取 X 数量的像素数据和换行符。如果任何读取函数返回错误代码,您应该打印出错误消息并中止。

https://en.wikipedia.org/wiki/Netpbm_format

关于c - 如何从 .pgm 文件读取像素值并将其复制到数组中。 C语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com