943,715 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4011
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 1st, 2008
0

help reading binary into a vector for manipulation?

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
Jul 1st, 2008
0

Re: help reading binary into a vector for manipulation?

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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 1st, 2008
0

Re: help reading binary into a vector for manipulation?

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
Reputation Points: 10
Solved Threads: 0
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
Jul 1st, 2008
0

Re: help reading binary into a vector for manipulation?

Click to Expand / Collapse  Quote originally posted by iansane ...
Any good books specifically for this topic?
Probably not. Just common sense (and experience)

Click to Expand / Collapse  Quote originally posted by iansane ...
When you say "while (in.get(&byt);
is "in" the name of the file stream?
Yes
Click to Expand / Collapse  Quote originally posted by iansane ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

Here is the correction.
C++ Syntax (Toggle Plain Text)
  1. char byt;
  2. while (file.get(byt))
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
iansane is offline Offline
26 posts
since Mar 2008
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

Click to Expand / Collapse  Quote originally posted by iansane ...
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() ).
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

Click to Expand / Collapse  Quote originally posted by iansane ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 2nd, 2008
0

Re: help reading binary into a vector for manipulation?

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:

C++ Syntax (Toggle Plain Text)
  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 …„ (
Reputation Points: 10
Solved Threads: 0
Light Poster
iansane is offline Offline
26 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dynamic allocation multi-dim array
Next Thread in C++ Forum Timeline: Help with Linked List Project!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC