read the header of a .pgm image file
I am attempting to read the size values from the header of a .pgm image
file (mars.pgm), and assign the resulting values to the integer variables
u and v using sscanf.
When executed the program prints P5 832 700 127 in the first line, which
is correct (the 832 and 700 are the size values that I want to pick out).
In the second line that is meant to print u and v variables two very large
numbers are printed, instead of the 832 and 700 values.
I cannot figure out why this is not working as desired. When using the
small test program (located at the bottom of the post) sscanf picks out
the values from a string like I expected it to.
#include<stdio.h>
#include <string.h>
int main()
{
FILE *fin;
fin= fopen ("mars.pgm","r+");
if (fin == NULL)
{
printf ("ERROR");
fclose(fin);
}
int u,v,i,d,c;
char test[20];
for (i=0; i<=20; i++)
{
test[i]=getc(fin);
}
sscanf(test,"%d,%d,%d,%d",&c,&u,&v,&d);
printf("%s\n",test);
printf("%d %d",u, v);
fclose(fin);
}
small test Program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
int a;
char s[3];
s[0]='1';
s[1]=' ';
s[2]='2';
sscanf(s,"%d",&a);
printf("%d",a);
}
No comments:
Post a Comment