Problem with read() function...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KWSW
    New Member
    • May 2007
    • 72

    Problem with read() function...

    Got an assignment where I need to convert between pgm and ppm. Now I know about the homework guideline so i went ahead to try to do up everything first and I managed to get it right. Or so it seems.

    The problem starts to come in when I try to convert bigger files. The top part of the image will be converted correctly but the bottom part would be left blank. As if I did not read in the full image info.

    what i did was to get the length of the data and create an unsigned char array to store it using the length. Next I would use the read() function to read into the array the info required

    file.read( reinterpret_cas t<char *>(img), (w)*sizeof(unsi gned char));

    But it seems that after a few read()s, it will just stop there and the rest will not be read in.

    Really hope someone can help here as I have spent the last day or so trying to figure it out.

    Thanks In Advance. :)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm confused. An unsigned char array has unsigned chars as elements. That is, the elements are one byte.

    Are you saying the array itself is larger enough to accommodate aby data item? And that you repeated load/unload thuis array as you read the file?

    How are you getting the lentgh of the data?

    I would have expected an fseek() from 0 origin to SEEK_END followed by a
    tell() to see how may bytes from 0 the file is. I would then read the entire file in one read() call.

    Comment

    • KWSW
      New Member
      • May 2007
      • 72

      #3
      nothing wrong with the array length...

      i even used the seekg and tellg to ensure the array length is correct and i am reading in the correct length...

      its like read() doesn't want to read in everything after awhile... :(

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Probably time to see a little more code.

        Comment

        • KWSW
          New Member
          • May 2007
          • 72

          #5
          Originally posted by weaknessforcats
          Probably time to see a little more code.
          my code:

          int pos = file.tellg();
          file.seekg(0,io s::end);
          int size = file.tellg() - pos;
          file.seekg(pos) ;

          img = (unsigned char*) new unsigned char[size];
          file.read( reinterpret_cas t<char *>(img), (size)*sizeof(u nsigned char));

          Comment

          • KWSW
            New Member
            • May 2007
            • 72

            #6
            Found this function gcount to which shows how many unformatted chars the previous input stream got.

            so i went and did:

            cout << " count: " << file.gcount() << " total: " << size << endl;

            well the numbers dun tally for big files that i have to read in... smaller files and they tally...

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This code:
              [code=cpp]
              int pos = file.tellg();
              file.seekg(0,io s::end);
              int size = file.tellg() - pos;
              file.seekg(pos) ;

              img = (unsigned char*) new unsigned char[size];
              file.read( reinterpret_cas t<char *>(img), (size)*sizeof(u nsigned char));
              [/code]

              looks odd.

              First, when seek to end of the file from an origin of 0, then the file length is return value from tellg() and not tellg() - pos, whatever pos is.

              Second, you are allocating an array of size elements, which is an int. Is size able to be stored in an int or have you exceeded the maximum int value?? You say things work until the file gets big. Maybe you need an unsigned int? Maybe the file needs to read in in swatches.

              Third, new return an unsigned char* pointer from new unsigned char[size]. That means you don't need the cast.

              Fourth, (size)*sizeof(u nsigned char) is really size * 1 or size.

              Fifth, it looks like img is an unsigned char* so I don't see how it converts to char*. Certainly, unsigned char values don't always fit in a char. Some of your data bits may be used as sign bits and that screws up the data.

              Comment

              • KWSW
                New Member
                • May 2007
                • 72

                #8
                Originally posted by weaknessforcats
                This code:
                [code=cpp]
                int pos = file.tellg();
                file.seekg(0,io s::end);
                int size = file.tellg() - pos;
                file.seekg(pos) ;

                img = (unsigned char*) new unsigned char[size];
                file.read( reinterpret_cas t<char *>(img), (size)*sizeof(u nsigned char));
                [/code]

                looks odd.

                First, when seek to end of the file from an origin of 0, then the file length is return value from tellg() and not tellg() - pos, whatever pos is.

                Second, you are allocating an array of size elements, which is an int. Is size able to be stored in an int or have you exceeded the maximum int value?? You say things work until the file gets big. Maybe you need an unsigned int? Maybe the file needs to read in in swatches.

                Third, new return an unsigned char* pointer from new unsigned char[size]. That means you don't need the cast.

                Fourth, (size)*sizeof(u nsigned char) is really size * 1 or size.

                Fifth, it looks like img is an unsigned char* so I don't see how it converts to char*. Certainly, unsigned char values don't always fit in a char. Some of your data bits may be used as sign bits and that screws up the data.
                thanks for the input... gonna give it one more try than its off to bed for me... its past 1am here...

                my last attempt:

                img = new unsigned char[width];

                /* convert unsigned chars to integers for processing */
                for(int i = 0; i < height; i++)
                {
                file.read(img, width);

                for(int j = 0; j < w; j++)
                {
                pVal = (int)img[j];
                /* other codes to store the pixel info */
                }

                }

                where width is the number of pixels in the width of the image and likewise for height.

                TIA for helping me :)

                Comment

                • KWSW
                  New Member
                  • May 2007
                  • 72

                  #9
                  darn still getting the same problem of read() not reading in everything even though the size to read and the buffer is correct...

                  Comment

                  • KWSW
                    New Member
                    • May 2007
                    • 72

                    #10
                    Originally posted by KWSW
                    darn still getting the same problem of read() not reading in everything even though the size to read and the buffer is correct...
                    hmm... managed to do a work around...

                    it seems that if i read in the binary block after i read in the header info of the pgm/ppm file, the read() will not read in everything if the file is too big.

                    Thus i read in the binary block first than go back to the start of the file to read in the header info...

                    Comment

                    Working...