Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:07:08+00:00 2026-05-26T20:07:08+00:00

So far I can read every line and print it out to the console:

  • 0

So far I can read every line and print it out to the console:

void readFile(){

   string line;
   ifstream myfile("example1.pgm");

   if (myfile.is_open()){
       while (myfile.good()){
         getline (myfile,line);
         cout << line;
       }
   }

However a pgm file apparently will always have the following at the start before the data:

P2
# test.pgm
24 7
15

How can i adapt my code so that it checks that “P2” is present, ignores any comments (#), and stores the variables and subsequent pixel data?

I’m a bit lost and new to c++ so any help is appreicated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:07:08+00:00Added an answer on May 26, 2026 at 8:07 pm

    There are a lot of different ways to parse a file. For something like this, you could look at the answers on this site. Personally, I would go with a loop of getline() and test/parse every line (stored in the variable “line”), you can also use a stringstream since it is easier to use with multiple values :

    Idea

    First line : test that P2 (Portable graymap) is present, maybe with something like

    if(line.compare("P2")) ...
    

    Second line : do nothing, you can go on with the next getline()

    Third line : store the size of the image; with a stringstream you could do this

    int w,h;
    ss >> w >> h;
    

    Following lines : store the pixel data until you reach the end of the file

    Resulting code

    You can try this code and adapt it to your needs :

    #include <iostream> // cout, cerr
    #include <fstream> // ifstream
    #include <sstream> // stringstream
    using namespace std;
    
    int main() {
      int row = 0, col = 0, numrows = 0, numcols = 0;
      ifstream infile("file.pgm");
      stringstream ss;
      string inputLine = "";
    
      // First line : version
      getline(infile,inputLine);
      if(inputLine.compare("P2") != 0) cerr << "Version error" << endl;
      else cout << "Version : " << inputLine << endl;
    
      // Second line : comment
      getline(infile,inputLine);
      cout << "Comment : " << inputLine << endl;
    
      // Continue with a stringstream
      ss << infile.rdbuf();
      // Third line : size
      ss >> numcols >> numrows;
      cout << numcols << " columns and " << numrows << " rows" << endl;
    
      int array[numrows][numcols];
    
      // Following lines : data
      for(row = 0; row < numrows; ++row)
        for (col = 0; col < numcols; ++col) ss >> array[row][col];
    
      // Now print the array to see the result
      for(row = 0; row < numrows; ++row) {
        for(col = 0; col < numcols; ++col) {
          cout << array[row][col] << " ";
        }
        cout << endl;
      }
      infile.close();
    }
    

    EDIT

    Here is a good tutorial on how to use stringstreams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every tutorial that I've read so far says that package name should be in
Every article on async programming in ASP.NET i've read so far states that while
So far I can understand HTML.ActionLink and URL.RouteURL but still trying to absorb where
Working on a carousel lightbox tooltip demo and as far I can see in
I'm new to Java, and while reading documentation so far I can't find any
I'm writing a program to generate some wild visuals. So far I can paint
As far as I can tell i'm doing everything by the book, but the
As far as I can tell, there is no API (official or unofficial) to
As far as I can tell, Scala has definitions for the Enumeration Value class
As far as I can tell, this code is fine, and should display some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.