| | |
help reading binary into a vector for manipulation?
![]() |
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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.
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)
//test reading binary of file for encryption // reading a complete binary file #include <iostream> #include <fstream> #include <vector> #include <stdio.h> //for atoi convert char to int #include <stdlib.h> using namespace std; vector<int> data; vector<int>::iterator iter; int size; char * memblock; int main () { ifstream file ("test.txt", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); for (int i = 0; i < size; ++i) { file.read (memblock, size); //testing cout << memblock; //had error: invalid conversion from char to int //and invalid conversion from char to char ? when using vector<char> //so I did it this way to convert to int and use vector<int> int newData = atoi (memblock); data.push_back(newData); } file.close(); cout << "the complete file content is in memory"; } else cout << "Unable to open file"; for (iter == data.begin(); iter != data.end(); ++iter) cout << *iter; cout << "\nPress Enter to exit.\n"; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }
Last edited by Ancient Dragon; Jul 1st, 2008 at 11:00 pm. Reason: add line numbers for convenience
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
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)
vector<unsinged char> data; ... ... unsigned char byt; while( in.get(&byt) ) { data.push_back(byt); }
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.
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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
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
Probably not. Just common sense (and experience)
Yes
Your program already did that. You don't have to change that open line, except remove the ios::ate as I already mentioned.
•
•
•
•
When you say "while (in.get(&byt);
is "in" the name of the file stream?
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.
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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.
Thanks
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)
//new binary read #include <iostream> #include <vector> #include <fstream> using namespace std; vector<char> data; vector<char>::iterator iter; int main() { ifstream file ("test.txt", ios::in|ios::binary); unsigned char byt; while (file.get(&byt)) { data.push_back(byt); } file.close(); for (iter == data.begin(); iter != data.end(); ++iter) cout << *iter; cout << "\nPress Enter to exit.\n"; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }
Thanks
Here is the correction.
C++ Syntax (Toggle Plain Text)
char byt; 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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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.
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() ). 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.
•
•
Join Date: Mar 2008
Posts: 25
Reputation:
Solved Threads: 0
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:
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)
ÿØÿà JFIF H H ÿá Exif MM * ÿÛ C ÿÛ CÿÀ à€" ÿÄ ÿÄ f !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€œ@ !' € ' èJ …„ (
![]() |
Other Threads in the C++ Forum
- Previous Thread: Dynamic allocation multi-dim array
- Next Thread: Help with Linked List Project!
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






