Reading a .jpg file in binary.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tawanda diza
    New Member
    • Sep 2011
    • 29

    Reading a .jpg file in binary.

    hie

    i am trying to read a picture(.jpg) file from my hard drive using Visual C++ 2008. i am trying to read it in binary format.

    My code is as follows:

    Code:
    int readTranHeader2 (void* buffer,int size)
    {
     FILE * pFile;
     int pos = 0;
     pFile = fopen ( "\\ResidentFlash\\P100040.JPG" , "rb" );
    
      fseek ( pFile ,pos , SEEK_SET );
      fread (buffer , 1 , size , pFile );
      fclose (pFile);
      return 0;
    }

    what i would like to ask is:
    Is my code correct?
    I don't know if i should use "r+b" instead.

    i would also like to ask if there is a way to read that file in HEX format.

    thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    No "rb" is fine, you would use "r+b" if you wanted ti be able to write to the file as well as read from it but you don't.

    i would also like to ask if there is a way to read that file in HEX format.
    This question has no meaning, HEX format, or DECIMAL format are just ways of displaying the data to the user on screen (or in a text file) and are irrelevant to the computer which holds everything internally in binary.

    The program could only read HEX format from a text file containing numbers written in hexadecimal, since jpgs are binary files (as already stated) not text files the question can not logically be answered.

    Comment

    • tawanda diza
      New Member
      • Sep 2011
      • 29

      #3
      Ok I understand now, its only that im new to system development,

      i was able to fix this. thanks Banfa.

      what i am currently looking on is how to read a jpg file up to the last byte, so that i willbe able to send all the bytes,

      Any Idea?

      Thanks In Advance

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Just call fread in a loop remember it returns the number of items read so on the last read you will just end up with less than the buffer size.

        Comment

        • tawanda diza
          New Member
          • Sep 2011
          • 29

          #5
          Thanks Banfa, i manged to find an alternative. instead i used the following code:

          Code:
          fp = fopen("\\ResidentFlash\\custphoto.bmp","rb");
          fseek(fp,0,SEEK_END); //go to end
          len = ftell(fp); //get position at end (length)
          fseek(fp,0,SEEK_SET); //go to beg.
          buf = (char *)malloc(len); //malloc buffer
          fread(buf,len,1,fp); //read into buffer
          fclose(fp);

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            That is OK but remember these 3 things about this method
            1. Seeking to the end of the file and using ftell to get the size only works if you have opened the file in binary mode (which you have) because of the way newlines are handled in text mode on Windows.
            2. Mallocing a buffer for the entire file will only work if the file is small enough to fit in memory, and remember that the computers hard disk is many times greater in size than its ram. However for jpg images this is probably reasonably safe.
            3. If you are going to use malloc then always test the return value before using it. malloc will return NULL if it can't allocate the memory.

            Comment

            • tawanda diza
              New Member
              • Sep 2011
              • 29

              #7
              Ooh that's great Advice Banfa,
              Thanks so much for the advice, i really appreciate this.

              Comment

              Working...