Thread: C++ Read PGM file

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    C++ Read PGM file

    Hello everyone,

    I am attempting to read a PGM file on standard input and print out the number of rows and columns that exist. I know that the file format begins with P2 and the next lines in the file are 235, 185, 255, then the pixels etc. I need to read these values and print them out using printf. I could use some help as to how to read in the values. Thanks for any help I can get.

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    fstream won't work? IF you know what to expect I would read in untill you get to the dimensions and just save the dimensions and close the fstream.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Well, I will be running on it several different files from the command line. Example: ./a.out < something.pgm and somethingelse.pgm.
    So I was thinking of fscanf or sscanf. Or if fstream will work could you post an example?

    **EDIT:
    I just wanted to mention I am new to c++. I have experience with java but not much in c or c++.
    Last edited by neogst; 02-06-2011 at 09:20 PM.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    If your doing command line you can pass it as an arg... I would think at least.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       ifstream infile;
       char *filepath;
       char var1[80];
       filepath = new char[strlen(argv[1])+1];
       strcpy(filepath,argv[1]);
       infile.open(filepath);
       infile>>var1;   //loop this to read next chunk of data
       return 0;
    
    }
    I don't know the PGM layout, but you can use this general code to read from a file. Whitespace will break the read by the way so we don't always grab 80 chars of data just seperate chunks this might help some.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Actually, I figured out how I wanted to read in the file. I just used a while loop and getline and stored the information in a string. How can I take the first few lines from my input and print them out?

  6. #6
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    just cout<<varname

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM