作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经被这个问题困扰了一段时间了。我如何将 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 数量的像素数据和换行符。如果任何读取函数返回错误代码,您应该打印出错误消息并中止。
关于c - 如何从 .pgm 文件读取像素值并将其复制到数组中。 C语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972397/
我是一名优秀的程序员,十分优秀!