Silentkiller
Technical User
Hi,
I need some help, i have written a program in C to read the header, width , height and maxVal of a pgm files.
I managed to read and display the header information, but failed to read the width, height and maxVal of the file. Can anyone enlighten me on what went wrong.
Here is my program:
/* Pgm.c */
#include <stdio.h>
#include <stdlib.h>
#include "pgm.h"
// #define Max 2000
int main( PGMImage *img)
{
int ch;
FILE *fp;
int type;
char string[80];
printf("Please enter the filename :"
;
scanf("%s",string);
if ((fp = fopen(string, "r"
)== NULL)
{
printf("Can'tOpen %s\n", string);
exit(1);
}
ch = getc(fp);
if (ch != 'P')
{
printf("ERROR(1) : Not Valid PGM File type\n"
;
exit(1);
}
printf("\n The filename is %s\n", string);
ch = getc(fp);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
;
}
printf("\n The value is %ld\n", type);
fseek(fp, -1, SEEK_CUR);
fscanf(fp,"%ld", &((*img).width));
fscanf(fp,"%ld", &((*img).height));
fscanf(fp,"%ld", &((*img).maxVal));
printf("\n width = %ld",(*img).width);
printf("\n height = %ld",(*img).height);
printf("\n maxVal = %ld" ,(*img).maxVal);
printf("\n"
;
return (0);
}
here is pgm.h
#ifndef PGM_H
#define PGM_H
#define Max 2000
/*
#define LOW_VALUE 0
#define HIGH_VALUE 255
*/
struct PGMstructure
{
int maxVal;
int width;
int height;
};
typedef struct PGMstructure PGMImage;
#endif
thank
I need some help, i have written a program in C to read the header, width , height and maxVal of a pgm files.
I managed to read and display the header information, but failed to read the width, height and maxVal of the file. Can anyone enlighten me on what went wrong.
Here is my program:
/* Pgm.c */
#include <stdio.h>
#include <stdlib.h>
#include "pgm.h"
// #define Max 2000
int main( PGMImage *img)
{
int ch;
FILE *fp;
int type;
char string[80];
printf("Please enter the filename :"
scanf("%s",string);
if ((fp = fopen(string, "r"
{
printf("Can'tOpen %s\n", string);
exit(1);
}
ch = getc(fp);
if (ch != 'P')
{
printf("ERROR(1) : Not Valid PGM File type\n"
exit(1);
}
printf("\n The filename is %s\n", string);
ch = getc(fp);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
}
printf("\n The value is %ld\n", type);
fseek(fp, -1, SEEK_CUR);
fscanf(fp,"%ld", &((*img).width));
fscanf(fp,"%ld", &((*img).height));
fscanf(fp,"%ld", &((*img).maxVal));
printf("\n width = %ld",(*img).width);
printf("\n height = %ld",(*img).height);
printf("\n maxVal = %ld" ,(*img).maxVal);
printf("\n"
return (0);
}
here is pgm.h
#ifndef PGM_H
#define PGM_H
#define Max 2000
/*
#define LOW_VALUE 0
#define HIGH_VALUE 255
*/
struct PGMstructure
{
int maxVal;
int width;
int height;
};
typedef struct PGMstructure PGMImage;
#endif
thank