reading 'null' from binary files into char arrays/strings
-
I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!
-
I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!
dfn wrote:
I assume this is so because one of the bytes I read (00) is treated like '\0' (null)
it's not just "treated like" a null, it is a null. C-style strings end on zero bytes. short answer: if your data contains 0's, don't treat it as a C string. long answer: if you need to read non-text data, you're going to have to read the data into byte arrays (char/u-char) and manipulate it there, with pointers - don't put it in std::strings or CStrings or anything like that because those objects are going to see those 0's and treat them as string terminators.
image processing toolkits | batch image processing | blogging
-
I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!
Are you using Unicode? If so, can you use
wstring
instead ofstring
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
dfn wrote:
I assume this is so because one of the bytes I read (00) is treated like '\0' (null)
it's not just "treated like" a null, it is a null. C-style strings end on zero bytes. short answer: if your data contains 0's, don't treat it as a C string. long answer: if you need to read non-text data, you're going to have to read the data into byte arrays (char/u-char) and manipulate it there, with pointers - don't put it in std::strings or CStrings or anything like that because those objects are going to see those 0's and treat them as string terminators.
image processing toolkits | batch image processing | blogging
Thanks for your help! I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string). I couldn't get unsigned chars to work with read(). Every attempt I made, trying to work with chars & strings, or chars only, had the same result.
-
Are you using Unicode? If so, can you use
wstring
instead ofstring
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Thanks for your help! I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string). I couldn't get unsigned chars to work with read(). Every attempt I made, trying to work with chars & strings, or chars only, had the same result.
dfn wrote:
I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string).
right, because binary data is not a string (strcat = string-concatenate). you are not going to be able to use any function or object that manipulates C-style strings - no "str*" functions, no "string" objects, no *scanf, or *printf functions.
image processing toolkits | batch image processing | blogging
-
dfn wrote:
I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string).
right, because binary data is not a string (strcat = string-concatenate). you are not going to be able to use any function or object that manipulates C-style strings - no "str*" functions, no "string" objects, no *scanf, or *printf functions.
image processing toolkits | batch image processing | blogging
-
I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!
you have to write a short function that change all 00 bytes in a so called "string" into the real string "0x00". To do this, read or copy your char array into a unsigned char array that is much longer. then manipulate this unsigned char array (change all 00 bytes into 4 bytes "0x00", then copy the unsigned char array back to your designated string.
-
I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!
Hi, I have to read a binary file in my projecto too. And then use the values of the binary to complete the names of my elements. I put you a bit of my code, I hope it helps
UINT nObj = 0, nSub = 0;
BYTE byteData = 0x00;
BYTE* pDataBuf;//For every object
for (nObj = 0; nObj < Header[6]; nObj++)
{ BYTE aReadSet [8] = {0};
pDataBuf = &aReadSet[0];
//For every subObject
for (nSub = 0; nSub < pDoc->m_cmlObjSet[nObj].m_cmlSubSet.GetCount (); nSub ++)
{ //Read the Data
file->Read (pDataBuf, 8 * sizeof (BYTE));
aReadSet[0] = *pDataBuf;
CString szNameUnit = "";
szNameUnit.Format (_T("%X"), aReadSet[0]);
pDoc->m_cmlObjSet[nObj].m_cmlSubSet[nSub].m_szSubName = "SubObject"+szNameUnit;
//Some Other operations with the other bytes in ReadArray
}
//More Code...
}With that I read every block of data, use the first member of the array (binary ID) to differenciate/identify every element is being saved... The IDs may have values from 00 to FA, and ALL are readen and written correctly. Ahm... I'm using VC++ 6
-------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
-
Hi, I have to read a binary file in my projecto too. And then use the values of the binary to complete the names of my elements. I put you a bit of my code, I hope it helps
UINT nObj = 0, nSub = 0;
BYTE byteData = 0x00;
BYTE* pDataBuf;//For every object
for (nObj = 0; nObj < Header[6]; nObj++)
{ BYTE aReadSet [8] = {0};
pDataBuf = &aReadSet[0];
//For every subObject
for (nSub = 0; nSub < pDoc->m_cmlObjSet[nObj].m_cmlSubSet.GetCount (); nSub ++)
{ //Read the Data
file->Read (pDataBuf, 8 * sizeof (BYTE));
aReadSet[0] = *pDataBuf;
CString szNameUnit = "";
szNameUnit.Format (_T("%X"), aReadSet[0]);
pDoc->m_cmlObjSet[nObj].m_cmlSubSet[nSub].m_szSubName = "SubObject"+szNameUnit;
//Some Other operations with the other bytes in ReadArray
}
//More Code...
}With that I read every block of data, use the first member of the array (binary ID) to differenciate/identify every element is being saved... The IDs may have values from 00 to FA, and ALL are readen and written correctly. Ahm... I'm using VC++ 6
-------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
-
you have to write a short function that change all 00 bytes in a so called "string" into the real string "0x00". To do this, read or copy your char array into a unsigned char array that is much longer. then manipulate this unsigned char array (change all 00 bytes into 4 bytes "0x00", then copy the unsigned char array back to your designated string.