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
vector<unsinged char> data;
...
...
unsigned char byt;
while( in.get(&byt) )
{
data.push_back(byt);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Any good books specifically for this topic?
Probably not. Just common sense (and experience)When you say "while (in.get(&byt);
is "in" the name of the file stream?
YesSo 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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Here is the correction.
char byt;
while (file.get(byt))
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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() ).
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
If you want to see the binary value of each byte then you will have to convert the bytes yourself because neither c nor c++ has build-in functions to do that (unfortunately). Here is an example program how to do that. You will probably want to leave the vector as an int vector and convert the data only for display purposes.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>BTW can you tell me what type of encoding is being used for the jpg?
Google for "jpg file format" and it will tell you the file format.
>>Now I just need to make an algorithm that encrypts and decrypts the stored data right?
Yes, just like the link I posted, but do that algorithm for every byte.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343