C 存储PPM P3文件的所有值
我正在“尝试”使用C存储P3 PPM文件,但我对它并没有太多经验,所以我遇到了麻烦。我想知道是否有人能帮我完成任务。多谢各位 我想将它们保留为struct-PPM和struct-PPM*getPPM(File*fd),但内容可以更改 我也不知道如何存储评论,我也想这样做 基本上,我尝试按如下方式存储数据:C 存储PPM P3文件的所有值,c,ppm,C,Ppm,我正在“尝试”使用C存储P3 PPM文件,但我对它并没有太多经验,所以我遇到了麻烦。我想知道是否有人能帮我完成任务。多谢各位 我想将它们保留为struct-PPM和struct-PPM*getPPM(File*fd),但内容可以更改 我也不知道如何存储评论,我也想这样做 基本上,我尝试按如下方式存储数据: P3 #comment.1 . . . #comment.n width height max r1 g1 b1 r2 g2 b2 r3 g3 b3 . . . 编辑: 我已经对其进行了修
P3
#comment.1
. . .
#comment.n
width height
max
r1 g1 b1
r2 g2 b2
r3 g3 b3
. . .
编辑: 我已经对其进行了修改,以便正确编译,现在我正在尝试传递argv,以便该文件可以读取argv[1]以获取文件名。当我这样做时,我收到一个分段错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Holds the data
struct data {int r,g,b;};
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;};
//Gets the PPM data
struct PPM* GetPPM(FILE * fd, argv)
{
char readChars[256] = {0};
//Allocates memory for PPM
struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM));
int i;
fgets(image->code, sizeof(image->code), fd);
fd = fopen(argv[1], "r");
//Checks if file is type P3
if((image->code[0] != 'P') && (image->code[0] != '3'))
{
return NULL;
}
image->code[2] = '\0';
//Checks for comments then continues around the loop until there's no more
fgets(readChars, sizeof(readChars), fd);
while(readChars[0] == '#')
{
fgets(readChars, sizeof(readChars), fd);
}
//Checks for PPM width, height and max
sscanf(readChars, "%d %d", &image->width, &image->height);
fgets(readChars, sizeof(readChars), fd);
sscanf(readChars, "%d", &image->max);
image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data));
i = 0;
while(fgets(readChars, sizeof(readChars), fd));
{
sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b));
++i;
}
fclose(fd);
return image;
}
//Deallocates memory
void freePPM(struct PPM *image)
{
free(image->Data);
free(image);
}
//Displays PPM
void showPPM(struct PPM *image)
{
int i = 0;
int totalpixels = image->width * image->height;
printf("%s\n", image->code);
printf("%d %d\n", image->width, image->height);
printf("%d\n", image->max);
for(i = 0; i < totalpixels; ++i)
{
printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b);
}
}
int main(int argc, char ** argv)
{
struct PPM *image = GetPPM(argv);
showPPM(image);
freePPM(image);
return 0;
}
#包括
#包括
#包括
//保存数据
结构数据{intr,g,b;};
结构PPM{char code[4];char注释;int width;int height;int max;struct data*data;};
//获取PPM数据
结构PPM*GetPPM(文件*fd,argv)
{
char readChars[256]={0};
//为PPM分配内存
struct-PPM*image=(struct-PPM*)calloc(1,sizeof(struct-PPM));
int i;
fgets(图像->代码,尺寸(图像->代码),fd);
fd=fopen(argv[1],“r”);
//检查文件是否为P3类型
如果((图像->代码[0]!='P')&&(图像->代码[0]!='3'))
{
返回NULL;
}
图像->代码[2]='\0';
//检查注释,然后继续循环,直到没有更多注释为止
fgets(读卡器、sizeof(读卡器)、fd);
while(readChars[0]='#')
{
fgets(读卡器、sizeof(读卡器)、fd);
}
//检查PPM宽度、高度和最大值
sscanf(读字符、%d%d、&image->width、&image->height);
fgets(读卡器、sizeof(读卡器)、fd);
sscanf(读字符、%d、&image->max);
图像->数据=(结构数据*)malloc(图像->宽度*图像->高度*大小(结构数据));
i=0;
而(fgets(readChars,sizeof(readChars),fd));
{
sscanf(readChars,“%d%d%d”,&(image->Data[i].r),&(image->Data[i].g),&(image->Data[i].b));
++一,;
}
fclose(fd);
返回图像;
}
//释放内存
void freepmm(结构PPM*图像)
{
免费(图像->数据);
免费(图像);
}
//显示PPM
void showPPM(结构PPM*图像)
{
int i=0;
int total像素=图像->宽度*图像->高度;
printf(“%s\n”,图像->代码);
printf(“%d%d\n”,图像->宽度,图像->高度);
printf(“%d\n”,图像->最大值);
对于(i=0;i数据[i].r,图像->数据[i].g,图像->数据[i].b);
}
}
int main(int argc,字符**argv)
{
结构PPM*image=GetPPM(argv);
showPPM(图像);
freepmm(图像);
返回0;
}
在声明结构和读取数据的方式时存在一些错误。在下面的代码中检查数据的读取方式,如果需要更清晰的信息,请返回此处
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data {int r,g,b;};
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;};
struct PPM* GetPPM(FILE * fd)
{
char readChars[256] = {0};
struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM));
int i;
fgets(image->code, sizeof(image->code), stdin);
if((image->code[0] != 'P') && (image->code[0] != '3'))
{
return NULL;
}
image->code[2] = '\0';
fgets(readChars, sizeof(readChars), stdin);
while(readChars[0] == '#')
{
fgets(readChars, sizeof(readChars), stdin);
}
sscanf(readChars, "%d %d", &image->width, &image->height);
fgets(readChars, sizeof(readChars), stdin);
sscanf(readChars, "%d", &image->max);
image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data));
i = 0;
while(fgets(readChars, sizeof(readChars), stdin))
{
sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b));
++i;
}
return image;
}
void FreePPM(struct PPM *image)
{
free(image->Data);
free(image);
}
void PrintPPM(struct PPM *image)
{
int i = 0;
int totalpixels = image->width * image->height;
printf("%s\n", image->code);
printf("%d %d\n", image->width, image->height);
printf("%d\n", image->max);
for(i = 0; i < totalpixels; ++i)
{
printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b);
}
}
int main()
{
struct PPM *image = GetPPM(stdin);
PrintPPM(image);
FreePPM(image);
return 0;
}
#包括
#包括
#包括
结构数据{intr,g,b;};
struct PPM{char code[4];char注释;int-width;int-height;int-max;struct-data*data;};
结构PPM*GetPPM(文件*fd)
{
char readChars[256]={0};
struct-PPM*image=(struct-PPM*)calloc(1,sizeof(struct-PPM));
int i;
fgets(图像->代码,尺寸(图像->代码),标准输入);
如果((图像->代码[0]!='P')&&(图像->代码[0]!='3'))
{
返回NULL;
}
图像->代码[2]='\0';
fgets(readChars,sizeof(readChars),标准DIN);
while(readChars[0]='#')
{
fgets(readChars,sizeof(readChars),标准DIN);
}
sscanf(读字符、%d%d、&image->width、&image->height);
fgets(readChars,sizeof(readChars),标准DIN);
sscanf(读字符、%d、&image->max);
图像->数据=(结构数据*)malloc(图像->宽度*图像->高度*大小(结构数据));
i=0;
while(fgets(readChars,sizeof(readChars,stdin))
{
sscanf(readChars,“%d%d%d”,&(image->Data[i].r),&(image->Data[i].g),&(image->Data[i].b));
++一,;
}
返回图像;
}
void freepmm(结构PPM*图像)
{
免费(图像->数据);
免费(图像);
}
无效打印PPM(结构PPM*图像)
{
int i=0;
int total像素=图像->宽度*图像->高度;
printf(“%s\n”,图像->代码);
printf(“%d%d\n”,图像->宽度,图像->高度);
printf(“%d\n”,图像->最大值);
对于(i=0;i数据[i].r,图像->数据[i].g,图像->数据[i].b);
}
}
int main()
{
结构PPM*image=GetPPM(stdin);
printpm(图像);
freepmm(图像);
返回0;
}
我只是想知道为什么您在代码中从未使用fd?如果PPM文件不能读取数据,我应该如何查看它的数据?或者只是将stdin的每个实例都更改为fd就可以解决这个问题吗?除此之外,它大部分看起来都不错,而且在大多数情况下,我对I/O的工作原理有了更好的理解now@Jason:GetPPM(FILE*fd)
函数接收FILE*
作为参数。因此,您可以始终打开所需的图像文件,并将其有效句柄传递给GetPPM
函数。