1. You have a binary file. Don't use getline to get data from it - getline gets a line from a text file; use read():
unsigned char key[20];
fstream f(".....",ios::binary);
// No need to open file explicitly
// when the filename is specified in the constructor.
if (f.is_open()) // or simply: if (f)
{
if (f.read((char*)key,sizeof key))
{
// you get this data!
}
}
The 1st arg of read() must be char* pointer, so use cast op if you want to read into unsigned char array.
2. Have a look at this (incorrect) code fragment:
std::string keystring;
...
unsigned char sequence [1];
memcpy(sequence,&keystring,1);
You never get the 1st byte of keystring contents! Right construct:
memcpy(sequence,keystring.data(),1);
An address (pointer to) of std::string object is not the same thing as this string data address. It's an address of a string object header. So you get a byte from this header but it's absolutely uninteresting result for you...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
I don't understand why you don't understand...
Look at my code snippet. Substitute your stream variable (myFile) instead of mine one (f).
if (myFile.read((char*)key,20))
{
// Well, you gOt your key! It's here, in key unsigned char array now!
// (char*)key - it's a proper cast here!
.... don't worry, process your key as unsigned char if you wish...
The stream.read() returns a reference to the stream. You can use a stream class object reference in conditional expression directly: it's true if a previous operation was successful. In actual fact there are a chain of casting here, but what does in matter to us?..
I hope, you feel at home not only in copy/paste environment...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Please, post your current code now (especially key writing part). You wrote a textual representation of repeated "0b" pair (exactly 30 62 in ASCII) instead of packed byte '0x0b').
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Wait a bit;): there are two different cases (let's use C++ notation):
1. mackeystring.dat contains TEXT data "0b0b... and so on 0b" (if so why it has .dat extension?)
2. mackeystring.dat contains BINARY data "\x0b\x0b...\x0b" (if so you can't print these 30 60 pairs).
In case#1 you must convert this data in binary form after reading (pack two byte values into a single byte). It's a simple limbering-up for beginner...
In case#2 you may place the file contents in the key array directly. No problems...
Happy end of the thread of this story?..
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Have you ever seen any C or C++ textbook?
In C and C++ "\x0b\x0b" denotes two-bytes string with byte value '\x0b' (11) - plus zero byte, but it's not interesting now.
Exactly, I want to know:
1. "the way you are storing the data in the file".
2. What did you want to store in the file - 20 bytes with values 13 (hexadecimal 0b) or 40 bytes of a text "0b0b...0b" - or what else?
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348