473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. int readTranHeader2 (void* buffer,int size)
  2. {
  3.  FILE * pFile;
  4.  int pos = 0;
  5.  pFile = fopen ( "\\ResidentFlash\\P100040.JPG" , "rb" );
  6.  
  7.   fseek ( pFile ,pos , SEEK_SET );
  8.   fread (buffer , 1 , size , pFile );
  9.   fclose (pFile);
  10.   return 0;
  11. }
  12.  

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.
Sep 23 '11 #1

✓ answered by Banfa

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.

6 17204
Banfa
9,065 Expert Mod 8TB
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.
Sep 23 '11 #2
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
Sep 26 '11 #3
Banfa
9,065 Expert Mod 8TB
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.
Sep 26 '11 #4
Thanks Banfa, i manged to find an alternative. instead i used the following code:

Expand|Select|Wrap|Line Numbers
  1. fp = fopen("\\ResidentFlash\\custphoto.bmp","rb");
  2. fseek(fp,0,SEEK_END); //go to end
  3. len = ftell(fp); //get position at end (length)
  4. fseek(fp,0,SEEK_SET); //go to beg.
  5. buf = (char *)malloc(len); //malloc buffer
  6. fread(buf,len,1,fp); //read into buffer
  7. fclose(fp);
  8.  
Sep 27 '11 #5
Banfa
9,065 Expert Mod 8TB
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.
Sep 27 '11 #6
Ooh that's great Advice Banfa,
Thanks so much for the advice, i really appreciate this.
Sep 27 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.