pillmill
asked on
binary string truncated ?
I need to read a 50 byte binary string from a file.
A function is invoked which is only returning a maximum 42 bytes:
method () {
CString GetString(const CString &src = "");
.... }
What is the problem ?
A function is invoked which is only returning a maximum 42 bytes:
method () {
CString GetString(const CString &src = "");
.... }
What is the problem ?
If the string is binary, it might contain NULL bytes, which automatically truncate the string at their position. Can you verify that?
ASKER
Yes - the truncation occurs when, intermitantly, a 0x0A appears in the string.
In this case, how can the entire string, including the NULL, be placed into the CString variable ?
In this case, how can the entire string, including the NULL, be placed into the CString variable ?
As stupid as it may sound - don't read a string, but a char array of 51 bytes instead. What is the underlying code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When reading with MFC classes you should open and read the file like
CFile file;
if (!file.Open("file.dat", CFile::typeBinary | CFile::modeRead))
{
AfxMessageBox("file.dat cannot be opened for read");
return;
}
unsigned char buffer[50] = { 0 }; // make all zeros
int count = file.Read(buffer, sizeof(buffer));
if (count == 0)
{
AfxMessageBox("file.dat cannot be read");
return;
}
With that buffer should contain all 50 bytes.
CFile file;
if (!file.Open("file.dat", CFile::typeBinary | CFile::modeRead))
{
AfxMessageBox("file.dat cannot be opened for read");
return;
}
unsigned char buffer[50] = { 0 }; // make all zeros
int count = file.Read(buffer, sizeof(buffer));
if (count == 0)
{
AfxMessageBox("file.dat cannot be read");
return;
}
With that buffer should contain all 50 bytes.