help reading binary into a vector for manipulation?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

help reading binary into a vector for manipulation?

 
0
  #1
Jul 1st, 2008
Hi, I am trying to learn how to read the binary of a file, such as a jpg. I got something going here but at run time in windows it says there is an error and the file must close.

I'm not even sure if I'm going in the right direction here. What I want to do is read the file on a binary level so I can make my own algorithm to encrypt and decrypt it. I know there are a lot of programs out there for that but this is for educational purposes.

Whether it's right or wrong way for me, can anyone tell me the right way to get the binary into a vector and then tell me if I should be doing it another way?

Thanks

Here's the code I have so far. I'm sure there are some major problems since I have confused myself and got lost.

  1. //test reading binary of file for encryption
  2.  
  3. // reading a complete binary file
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. #include <stdio.h> //for atoi convert char to int
  9. #include <stdlib.h>
  10.  
  11.  
  12. using namespace std;
  13. vector<int> data;
  14. vector<int>::iterator iter;
  15.  
  16.  
  17.  
  18. int size;
  19. char * memblock;
  20.  
  21. int main () {
  22. ifstream file ("test.txt", ios::in|ios::binary|ios::ate);
  23. if (file.is_open())
  24. {
  25. size = file.tellg();
  26. memblock = new char [size];
  27. file.seekg (0, ios::beg);
  28. for (int i = 0; i < size; ++i)
  29. {
  30. file.read (memblock, size);
  31.  
  32. //testing
  33. cout << memblock;
  34.  
  35. //had error: invalid conversion from char to int
  36. //and invalid conversion from char to char ? when using vector<char>
  37. //so I did it this way to convert to int and use vector<int>
  38. int newData = atoi (memblock);
  39. data.push_back(newData);
  40. }
  41.  
  42.  
  43.  
  44. file.close();
  45.  
  46. cout << "the complete file content is in memory";
  47.  
  48.  
  49. }
  50. else cout << "Unable to open file";
  51.  
  52. for (iter == data.begin(); iter != data.end(); ++iter)
  53. cout << *iter;
  54.  
  55. cout << "\nPress Enter to exit.\n";
  56. cin.ignore(cin.rdbuf()->in_avail() + 1);
  57.  
  58. return 0;
  59. }
Last edited by Ancient Dragon; Jul 1st, 2008 at 11:00 pm. Reason: add line numbers for convenience
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help reading binary into a vector for manipulation?

 
0
  #2
Jul 1st, 2008
line 22: The ios::ate flag does nothing for input files. Its use it intended for output files.

line 19 and 26: memblock should be unsigned char because a binary byte can be any value between 0 and 255.

line 33: can't do that with a binary file. cout expects a string or POD (Plain Old Data). A binary blob is none of those. atoi() takes a const char as a parameter, and a binary blob is not that data type.


line 38: Can't do that either.

line 39: That one's out too. data is a vector of integers and you are attempting to push a char*. Wrong data type.

line 52: Since line 39 is useless so is that loop beginning at line 52.

Ok, so now that I gave you all (or most) of the negatives, how can you improve it? One way is to just simply read the file one byte at a time and add it to the linked list
  1. vector<unsinged char> data;
  2. ...
  3. ...
  4. unsigned char byt;
  5. while( in.get(&byt) )
  6. {
  7. data.push_back(byt);
  8. }
Last edited by Ancient Dragon; Jul 1st, 2008 at 11:17 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: help reading binary into a vector for manipulation?

 
0
  #3
Jul 1st, 2008
Any good books specifically for this topic?

When you say "while (in.get(&byt);
is "in" the name of the file stream?
So I just need to open a fstream called "in" or whatever and use it that way to read one byte at a time?

Thanks for the help Ancient Dragon

I get confused by a lot of the stuff at cplusplus.com and don't understand why it doesn't work till someone like you tells me why.

Thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help reading binary into a vector for manipulation?

 
0
  #4
Jul 1st, 2008
Originally Posted by iansane View Post
Any good books specifically for this topic?
Probably not. Just common sense (and experience)

Originally Posted by iansane View Post
When you say "while (in.get(&byt);
is "in" the name of the file stream?
Yes
Originally Posted by iansane View Post
So I just need to open a fstream called "in" or whatever and use it that way to read one byte at a time?
Your program already did that. You don't have to change that open line, except remove the ios::ate as I already mentioned.
Last edited by Ancient Dragon; Jul 1st, 2008 at 11:43 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: help reading binary into a vector for manipulation?

 
0
  #5
Jul 2nd, 2008
Ok I simplified the whole thing. Now I have an error for "while (file.get(&byt)".

error: 16 X:\testingBinaryFileRead\newBinaryRead.cpp no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::get(unsigned char*)'

I don't understand the error or how to fix it.

  1. //new binary read
  2.  
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. vector<char> data;
  10. vector<char>::iterator iter;
  11.  
  12. int main()
  13. {
  14. ifstream file ("test.txt", ios::in|ios::binary);
  15. unsigned char byt;
  16. while (file.get(&byt))
  17. {
  18. data.push_back(byt);
  19.  
  20.  
  21. }
  22. file.close();
  23.  
  24. for (iter == data.begin(); iter != data.end(); ++iter)
  25. cout << *iter;
  26.  
  27.  
  28.  
  29. cout << "\nPress Enter to exit.\n";
  30. cin.ignore(cin.rdbuf()->in_avail() + 1);
  31.  
  32. return 0;
  33. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help reading binary into a vector for manipulation?

 
0
  #6
Jul 2nd, 2008
Here is the correction.
  1. char byt;
  2. while (file.get(byt))
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: help reading binary into a vector for manipulation?

 
0
  #7
Jul 2nd, 2008
why did unsigned not work and what about the &?

It still gives "error...program has to close" unless I put the ios::ate back in. For some reason, that makes it stay open, but it's not printing out anything. Like the vector is empty.

I'll keep working on it.
I have to go for a while.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: help reading binary into a vector for manipulation?

 
0
  #8
Jul 2nd, 2008
Originally Posted by iansane View Post
It still gives "error...program has to close" unless I put the ios::ate back in. For some reason, that makes it stay open, but it's not printing out anything. Like the vector is empty.
Didn't your compiler complain about the following line?
for (iter == data.begin(); iter != data.end(); ++iter)
// it should be ...
for (iter = data.begin(); iter != data.end(); ++iter)

Since you opened with ios::ate , the vector ended up empty because there was nothing to read in the first place. And the iterator being a global variable was initialized to NULL, so the for() loop was skipped altogether (i.e. iter was initially equal to data.end() ).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help reading binary into a vector for manipulation?

 
0
  #9
Jul 2nd, 2008
Originally Posted by iansane View Post
why did unsigned not work and what about the &?
The get() function is only defined for char, not unsigned char. And the parameter is a reference to a char so the & is not useful there.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: iansane is an unknown quantity at this point 
Solved Threads: 0
iansane iansane is offline Offline
Light Poster

Re: help reading binary into a vector for manipulation?

 
0
  #10
Jul 2nd, 2008
I feel like a dummy. That part with "iter ==" was a common mistake in c++ class that I usually spotted and corrected in other peoples code. Thanks for pointing it out.

It works now but I thought I would see 1's and 0's.

The output for a .jpg is the same bunch of un-decypherable symbols I see when I open it with notepad.

What is the next step if I want to see it in binary? Can you tell me about breaking it down that far?(decoding it) And what type of encoding is it that I am seeing now?

I've practiced 64 bit encoding with paper and pencil so I'm thinking if I know the encodeing and had a chart to go by, I could figure it out.

sample:

  1. ÿØÿà JFIF H H ÿá Exif MM * ÿÛ C ÿÛ CÿÀ à€" ÿÄ
  2. ÿÄ f
  3.  
  4. !1AQaÁð$q‘¡±á4Ñ%Dñ 5Td‚&ERt¢6U„ÂÒâ"#3BCbe¤´FSVWc’”ÄÔ'2suv…•–7GfµÿÄ ÿÄ D !1AQaq‘¡ð±ÁÑáñ"$2BRb‚4Dr’²ÂÒ¢â#3Ã%ÿÚ ? 一…3€( ] ¼'8™Âpð0™Àp!7€( ]0›Âpð ¼'8 ¼@P&ðœ8át&ð œ „œ@ „œ@ „œ@ ˜Mà pœ( “€( 8€ 8€œ@ !' € '
  5. èJ …„ (
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC