Hopefully this helps:
Code:
typedef struct Image {
char magval;
int width;
int height;
int maxval;
char *data;
} Image;
void Q_readPPMHeader(FILE *fp, char magval, int *width, int *height, int *maxval)
{
char ch;
if ((fscanf(fp, "P%c\n", &ch) != 1) && (ch != '6' || ch != '5' || ch !='3' || ch != '2'))
Q_error("Cannot read either .PPM or .PGM file");
sprintf(magval, "P%c", ch); /**ISSUE IS HERE**/
printf("magic no: %s\n", magval);/**ISSUE IS HERE**/
/* skip comments */
ch = getc(fp);
while (ch == '#') {
do {
ch = getc(fp);
} while (ch != '\n'); /* read to the end of the line */
ch = getc(fp);
}
if (!isdigit(ch)) Q_error("cannot read header information from ppm file");
ungetc(ch, fp); /* put that digit back */
/* read the width, height, and maximum value for a pixel */
fscanf(fp, "%d%d%d\n", width, height, maxval);
if (*maxval != 255) Q_error("image is not true-color (24 bit); read failed");
printf("width: %d\n", *width);
printf("height: %d\n", *height);
printf("maxval: %d\n", *maxval);
}
Image *Q_ImageRead(char *filename)
{
int width, height, maxval, size;
char magval; /**ISSUE IS HERE**/
Image *image = (Image *) malloc(sizeof(Image));
FILE *fp = fopen(filename, "r");
if (!image) Q_error("cannot allocate memory for new image");
if (!fp) Q_error("cannot open file for reading");
Q_readPPMHeader(fp, &magval, &width, &height, &maxval);
size = width * height;
image->magval = magval;
image->width = width;
image->height = height;
image->maxval = maxval;
image->data = (char *) malloc(size);
if (!image->data) Q_error("cannot allocate memory for new image");
fread((char *)image->data, 1, (size_t) size, fp);
fclose(fp);
return image;
}