How to read .pgm format file?

I'd like to know how to read the .pgm (pixel gray map) file, for the one who doest'nt know

1
2
3
4
5
6
7
8
P2
# This is an example called j.pgm
6 5
255
0 0 0 0 255 70
0 0 0 0 255 50
0 0 0 0 255 200
0 0 0 0 100 10 


Line 1 - The version
Line 2 - The comment
Line 3 - The width and height
Line 4 - The gray level
Line 5 to end - The pixels.

I wrote the file in the Notepad, and I try to read it with fscanf with char[] (string) type but when i print it on the screen it only appears mad symbols, what is the problem? Thanks!
Don't use fscanf() if you can help it. But your fscanf would look like
fscanf(file, "%i %i %i %i %i %i", int1, int2, int3, int4, int5);
Nasty =\

I'd read the line in using getline() on the iostream (don't use C file types). Then split the line based on space-char into an array.
Sorry, I didn't understand it very much, this getline() function as I read doesn't have a file type argument...And the data type to read the lines, do I have to use array strings or pointer strings? Thx
Here is the code i did...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

void func(){
    FILE *pArq;
    pArq = fopen("j.pgm", "ab");

    char line1[2], line2[100], line3[10];

    int cont = 1;
    while(1){
        if(cont ==1){ //version
            fscanf(pArq, "%s", &line1);
            if(feof(pArq)) break;

            printf("%s", line1);
        }

        if(cont ==2){ //comment
            fscanf(pArq, "%s", &line2);
            if(feof(pArq)) break;

            printf("%s", line2);
        }

        if(cont ==3){ //width, height
            fscanf(pArq, "%s", &line3);
            if(feof(pArq)) break;

            printf("%s", line3);
        }

        cont++;
    }
    fclose(pArq);
}
Last edited on
From the home page of this site there is a link to an excellent collection of tutorials. I suggest you read the sections titled Basic Input/Output and Input/Output with Files. They are replete with examples.

I suggest that you use getline() to read an entire line at a time, then use a stringstream to collect the values from it. An unsigned short is the smallest data size that will conform with the PGM format's value range.

How do you intend to display the image? (What OS are you using?) Because you cannot display it in the terminal -- you have to be running a GUI of some type.

How you store the image depends on how you intend to display it. For example, if you are programming on MS Windows, you should stick the pixel values into a BITMAP structure.

The other possibility is the create your own image format and draw the image yourself (pixel by pixel) to the output device. An example:
http://www.cplusplus.com/forum/general/2236/
(see my post at the bottom).

Either way, you'll have to spend some time learning how to do it...

Good luck.
I still didn't get it how to use this getline(), it's atributes are (FILE, char/string)?

And about this, i just want to make a program to modify the pixels of the file...
Last edited on
There are two versions of getline(). You want the <string> version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  string       s;     // each line entered by the user
  stringstream outs;  // stuff we'll tell the user after all input is received

  cout << "Please enter some numbers. Press ENTER twice to finish.\n";

  // get lines so long as 1) there is input and 2) the last line entered is not ""
  int line_number = 1;
  while (getline( cin, s ) and !s.empty())
    {
    // count the number of numbers in the line
    double d;
    stringstream ss( s );
    int count = 0;
    while (ss >> d) count++;

    // this is what we'll tell the user when all done
    outs << "On line " << line_number << " you entered " << count << " numbers.\n";
    }

  // print out all our commentary
  cout << outs.str();
  return 0;
  }


There are two reasons to use getline( infile, ... ) over just infile >> ... : 1) you can detect and recover better from errors, and 2) you can recognize and discard #commentary.

Hope this helps.
Topic archived. No new replies allowed.