P2
5 7
255
100 114 111 100 112
90 110 112 110 111
90 110 124 110 119
80 110 110 110 118
90 110 100 110 109
111 110 163 110 120
100 98 111 145 112
I need to read this pgm file in C using fscanf
I have managed to read line one and store the P2 in a variable called p2 I'm stuck on reading (line 2 integer 1) and (line 2 integer 2) which are 5 & 7 I want to store read them and store them in variables row and col
Next I want to read in line 3 "255" and store that also
Them I need to read Everything from line 4 onward and store all the rest of the greyscale values in a variable greyscale.
I understand everything I need to read the file except reading each line and integer using fscanf can someone help here is my code so far
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char p2[2];
int row[3];
int col[3];
int max[4];
int greyscale[35];
int i;
FILE *input_fptr;
if ((input_fptr = fopen("image1.pgm", "r")) == NULL)
{
printf("ERROR! File cannot be opened.");
exit(1);
}
// Read in Line 1 using fscanf
fscanf(input_fptr, "%s", p2);
printf("Line 1 data is: %s\n", p2);
// Read in Line 2 rows and colulmns
while ((fscanf (input_fptr, "%d", &row)) == 3)
{
printf("%d", row);
}
printf("\n");
fclose(input_fptr);
return 0;
}