Thread: problem with get()

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    problem with get()

    Hi,
    I'm making an image editor and I'm having problems with the get() command while reading a file.

    First, here's the part of the code I have problems with :
    Code:
    //image.h
    class Image{
       public:
       //...
       protected:
          int fileType, 
              width,
              height;
              float** tab;
              //...
    }
    //image.cpp
    float Image::readChar(ifstream& input){
       char charTemp;
       float floatTemp;
       input.get(charTemp);
       floatTemp = (float) charTemp;
       //...
       return floatTemp;
    };
    void Image::readImage(ifstream& input){
       readHeader(input); //this reads the header of the file
       int k, l;
       tab = new float*[getHeight()];
       for(int j=0; j<height; j++) tab[j] = new float[getWidth()];
    
       if(fileType == 5 || fileType == 6){
          for(k = 0; k<height; k++)
             for(l = 0; l<width; l++){
                tabR[k][l] = readChar(input);
             }
       }
       //...
    };
    The file i'm reading is a pgm (portable greymap).
    The problem is when I try to read a file witch have the 26th ASCII character, input.get(charTemp) gets the ASCII number 31 and then act as if the file has ended (even if there's more than 200 ASCIIs left in the file after that character).
    example : ")#¬" would be read as 41, 35, 31, 204, 204, 204 instead of 41, 35, 31, 26, 29, 172
    (I tested a few thing and input.eof() always returns true)
    Anyone have an idea of why this is happening and what I should do?
    Thanks
    Jonathan

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Anyone have an idea of why this is happening and what I should do?
    Did you open the file in binary mode?
    Code:
    ifstream input(filename, ios::binary);

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    no, I guess I'll try that...
    thanks

    EDIT: nice, it works, thanks a lot. Still, any idea of why that was happening while not in binary mode?
    Last edited by Kugans; 03-09-2006 at 09:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM